View Full Version : [java] manipulate a string
mickey
10th July 2008, 14:11
Hello,
I have a string containing a fileName and I have to cut the .extension if it there is. otherwise do nothing; furthermore I have to change the first letter to uppercase:
Is this ok?
filename "file.xml" -> File
filename "file" -> File
I do it but it's not comple......... and it works only string with .extension
String s = fileName.substring(0, fileName.indexOf(".")
Any hints, please?
MrShahi
10th July 2008, 14:14
Then whats your problem ?????
can you explain it clearly...............
mickey
10th July 2008, 16:57
The problem is I'm new to java.
Is my way to take out the ".xml" the best way? According to me, if fileName doens't contain an extension (eg. it's only "file") that code doesn't work.
Furthermore how can I change the first letter of the fileName to upper letter, please?
Thanks,
wysota
10th July 2008, 19:02
How about:
String s = fileName;
if(fileName.indexOf(".")>0)
s = fileName.substring(0, fileName.indexOf(".");
Just be aware it won't work for file names like "abc.def.ghi". Using lastIndexOf() if java strings have such a thing would be better.
mickey
10th July 2008, 20:24
and what do you think for the second question? Can this be right?
String name = fileName;
if( name.indexOf(".")>0 )
name = name.substring(0, fileName.indexOf("."));
name = name.substring(0,1).toUpperCase() + name.substring(1, name.length() );
something simpler?
wysota
10th July 2008, 21:22
It's fine, provided that toUpperCase() can handle the conversion in every case (such conversions sometimes fail with national characters or encodings).
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.