Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting last piece of string  RSS feed

 
dale conn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you familiar with the split method?
 
Manhar Puri
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do i use the length method
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. The split method doesn't require length.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dale conn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mean file.getName()
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!