Results 1 to 15 of 15
  1. #1
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default How can i get substring

    There is String
    a = "/store/assets/images/products/Rings/SHPTH7009_OV_1_Small.jpg";


    I want to get 'SHPTH7009' from this string .How can i get ?

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    freedom exists in the world of ideas

  3. #3
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    In order to extract the substring from a string,you can use the split method
    So this is what you really need i think:
    Java Code:
    public class Shell {
                           private String string="/store/assets/images/products/Rings/SHPTH7009_OV_1_Small.jpg";
                           private String[] split=string.split("[/_]");
    
                           public static void main(String[] args) {
                                   System.out.println(new Shell().split[6]);}
    }
    And the output will be:
    Java Code:
    SHPTH7009
    I hope i helped you.
    Last edited by serjant; 06-23-2008 at 03:33 PM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    But this is failed if the path too long. I mean number of slashes issue. Since the path define is unchanged this is fine. ;)

  5. #5
    ilysony's Avatar
    ilysony is offline Member
    Join Date
    Jun 2008
    Location
    China
    Posts
    3
    Rep Power
    0

    Default

    serjant
    doing great!

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes I agreed with that. I'm not saying it is bad practice at all. I'm saying is what happened if the pattern of the path is changed.

  7. #7
    c26354 is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    It depends on how much you know about the path before hand.

    A good way of doing this is with the regex package:

    Java Code:
    import java.util.regex.*;
    Then you would create a pattern and a matcher:

    Java Code:
    //(.*) - the .* means anything and the () means a capture group
    Pattern pat = Pattern.compile("/(.*).jpg");
    Matcher match = pat.matcher(a);   //a is the string to match
    
    String fileName = match.group(1);   //this will return everything from the (.*)

    now you have the file name. The advantage of doing this is that you can be way more specific about what part to get. If you don't want the part after the _ then use the split method given by serjant.

  8. #8
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Here is my solution ^_^

    Java Code:
    String a = "/store/assets/images/products/Rings/SHPTH7009_OV_1_Small.jpg";
    String temp = a.substring(a.lastIndexOf('/')+1);
             temp = temp.substring(0, temp.indexOf('_'));
              System.out.println(temp);
    I cut the left side of the string from the last '/' including the slash and cut the rigth side of the String from '_' to the last and thats how i extracted the SHPTH7009

    I hope that helps
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Eku View Post
    Here is my solution ^_^

    Java Code:
    String a = "/store/assets/images/products/Rings/SHPTH7009_OV_1_Small.jpg";
    String temp = a.substring(a.lastIndexOf('/')+1);
             temp = temp.substring(0, temp.indexOf('_'));
              System.out.println(temp);
    I cut the left side of the string from the last '/' including the slash and cut the rigth side of the String from '_' to the last and thats how i extracted the SHPTH7009

    I hope that helps
    I give my points to this. Best way to do it. Reason to use this is, no need to import addition packages to my application. So that package linker times is reduce. I really like that.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by c26354 View Post
    It depends on how much you know about the path before hand.

    A good way of doing this is with the regex package:

    Java Code:
    import java.util.regex.*;
    Then you would create a pattern and a matcher:

    Java Code:
    //(.*) - the .* means anything and the () means a capture group
    Pattern pat = Pattern.compile("/(.*).jpg");
    Matcher match = pat.matcher(a);   //a is the string to match
    
    String fileName = match.group(1);   //this will return everything from the (.*)

    now you have the file name. The advantage of doing this is that you can be way more specific about what part to get. If you don't want the part after the _ then use the split method given by serjant.
    So importing another package you have to find the file name first. Then you have to split it again to find the correct string. Why you use such a long way, for me this is too much for such a simple thing.

    And also for me, your pattern is wrong. I don't think it can find the file name. Check the pattern (.*) there.

  11. #11
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    thanks for the appreciation eranga. I had met this kind of problem before. Experience really teaches me here ^_^
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I'm cool here Eku. First thing is really like to give what I know to any other person. I love that. If I feel that something is good, in my experience, I appreciate it. :)

    That mean experience is the best in your life, plays a major role.

  13. #13
    c26354 is offline Member
    Join Date
    Jun 2008
    Posts
    11
    Rep Power
    0

    Default

    Good catch, the (.*) will be greedy, so you would need (.*?) instead.

    The (.*?) will give you "SHPTH7009_OV_1_Small". Instead of that you could make the pattern "/(.*?)_" and it will get only "SHPTH7009".

    Of course its better if you don't have to use any other packages, but this is a very general way of doing this, and it give lots of flexibility. I guess its mostly only useful if you don't know a lot about the file. For example, what if there is a directory that contains a "_". That might cause problems.

    Regex is a helpful package to know, but not needed in this case.

  14. #14
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    yeah got your point c26354 ^_^. There are a total of two cutting process.
    1. Eliminating the directory from the String
    2. Getting the substring from index 0 of Temp to the index of the first "_" in Temp

    So there will no problem if there is a "_" in the directory it will still cut the from the last '/'. The only problem i see in that is if there is a '/' in the filename which is not that usual.

    But still regex is much useful in most complex cases but not in this one.
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    c26354 and Eku,

    As both of you said, regex is much better much useful package in Java, not in a simple project like this. Regular expressions used in depth in complex projects.

    Such a simple case, it's useless for me. As I said in my last post, to get the correct information you have to do few things. For me it's too much. :)

Similar Threads

  1. Read-File Write Display substring
    By hiklior in forum New To Java
    Replies: 3
    Last Post: 04-18-2008, 11:45 AM
  2. How to use subString method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:44 PM
  3. help me with a realy easy program (substring)
    By michcio in forum New To Java
    Replies: 7
    Last Post: 01-27-2008, 12:41 AM
  4. String substring function
    By ravian in forum New To Java
    Replies: 6
    Last Post: 01-02-2008, 07:35 PM
  5. substring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:15 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
  •