| Author |
getting last piece of string
|
dale conn
Ranch Hand
Joined: Jun 16, 2006
Posts: 57
|
|
dear all i have a string like this http:\\mydomain.com\foldername\subfoldername\filename.xml it won't always have the same number of slashes how can i get the last piece of data, everything after the last slash thanks for you help
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
Are you familiar with the split method?
|
 |
Manhar Puri
Ranch Hand
Joined: Aug 23, 2005
Posts: 41
|
|
Dale, Split the string based on the slash. There is a split function in java.lang.String class. This function will return the an array of strings. The last element of the array is the String u want. -Manhar.
|
 |
dale conn
Ranch Hand
Joined: Jun 16, 2006
Posts: 57
|
|
i know how to split the string, i just don't how to get the last element String s = "http:\\\\mydomain.com\\foldername\\subfoldername\\filename.xml"; s.split("\\");
|
 |
dale conn
Ranch Hand
Joined: Jun 16, 2006
Posts: 57
|
|
|
do i use the length method
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
No. The split method doesn't require length.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32669
|
|
|
I think he means the length attribute of the array created from the split() method, used to find the last token from the array. In which case, yes. The last token will be splitString[splitString.length - 1].
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
split() returns a String array. You want the last element of the array. Of course, if you just want to get the file name from a path, you should create a File object and then call its getName() method. This will be more o/s independent as you will not need to worry what the path separator is.
|
Joanne
|
 |
dale conn
Ranch Hand
Joined: Jun 16, 2006
Posts: 57
|
|
thanks joanne, this is better than split File file = new File("http:\\\\mydomain.com\\foldername\\subfoldername\\filename.xml"); System.out.println(file.getAbsoluteFile());
|
 |
dale conn
Ranch Hand
Joined: Jun 16, 2006
Posts: 57
|
|
|
mean file.getName()
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
Urg, so many issues. File.getName() might work if the thing you're talking about has a path like a file. Since it begins with http: though that looks suspicious and misleading to me. On the other hand, the fact that this thing that looks like a URL has backslashes rather than forward slashes is also suspicious. Are you sure that's really what you want here? If the backslashes are correct, then using split() is more complicated than it looks because \ is a special character for both Java and for regular expressions, and there are two levels of escaping required. (Yes, when you use split() you're using a regular expression.) Which means that to split over a literal \, you actually need split("\\\\"). But if it's actually / you should be using, then split("/") is sufficient. If the reasons for this are not apparent, I recommend studying regular expressions. It may be more complexity than you really need for this particular problem, but regular expressions are also very useful in general, so it's beneficial for you to learn about them eventually. Or, you could just forget all that and use a few simple methods of the String class:
|
"I'm not back." - Bill Harding, Twister
|
 |
 |
|
|
subject: getting last piece of string
|
|
|