• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

getting last piece of string

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you familiar with the split method?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do i use the length method
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The split method doesn't require length.
 
Marshal
Posts: 75878
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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].
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mean file.getName()
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic