string - How to change the dot (if more than 2) in java? -
i'd change dot ""
if more 2. mean hel.lo.txt
or he.l.l.o.txt
returns hello.txt
.
i wrote not begin end "."
int pointindex = filename.indexof("."); if (pointindex==0){ system.out.println("point index :"+pointindex); string mystring=filename.substring(pointindex, 1); filename=filename.replace(mystring, ""); } //end "." int extensions = filename.lastindexof("."); string newfilename=filename.substring(extensions); system.out.println(newfilename.length()); if ( newfilename.length()<2){ system.out.println("dont have extension, filename end is"+"."); int dotidx=filename.lastindexof("."); filename=filename.substring(0,dotidx); system.out.println(filename); }
you can manipulate string
here
string str = "he.l.l.o.txt"; int lastindexofdot = str.lastindexof('.'); string str1 = str.substring(0, lastindexofdot + 1); string str2 = str.substring(lastindexofdot, str.length()); str = str1.replace(".", "") + str2; system.out.println(str);
output :
hello.txt
note: can create method take string
argument , return required string
.
Comments
Post a Comment