Results 1 to 7 of 7
  1. #1
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default How to withdraw a specific from a String?

    Hi, i have a "windows URL" and I want to take the last thing in that url and save in a String.

    The url is like
    C:\Music\Song.mp3
    I've come up with this:

    String name[] = urlString.Split("'\'");
    System.out.println(name[0]);
    Which gives me:
    "C:\Music\Song.mp3"

    But what can I use to save "Song.mp3" to a string?

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by equal View Post
    But what can I use to save "Song.mp3" to a string?
    Does a lastIndexOf("\\") help?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Does a lastIndexOf("\\") help?

    kind regards,

    Jos
    Tried: String test = urlString.lastIndexOf("\\");

    But it wants to change String to int. And when I do that it returns "2".

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    That's because lastIndexOf returns an int. Then you can use that int to get the proper subString.

    You could also still split the String, but realize that the parameter to the split method is a regular expression, and so you need to use "\\\\":

    Java Code:
          String urlString = "C:\\Music\\Song.mp3";
          String[] tokens = urlString.split("\\\\");
          System.out.println(tokens[tokens.length - 1]);
    Last edited by Fubarable; 05-29-2011 at 09:51 PM.

  5. #5
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    That's because lastIndexOf returns an int. Then you can use that int to get the proper subString.
    Ok, sorry, but how do I do that?

  6. #6
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanks, I got it all sorted now!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by equal View Post
    Ok, sorry, but how do I do that?
    That's why the API documentation exists; to prevent users/programmers from trying random guesses. Read it. (the lastIndexOf( ... ) method is in the String class).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. No of a specific char in a string
    By alinaqvi90 in forum New To Java
    Replies: 13
    Last Post: 05-27-2010, 08:59 AM
  2. Replies: 5
    Last Post: 01-17-2010, 12:14 AM
  3. HashMaps get key for a specific value
    By andre1011 in forum Advanced Java
    Replies: 2
    Last Post: 03-11-2009, 03:30 AM
  4. Replies: 2
    Last Post: 11-08-2008, 11:40 AM
  5. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 02:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •