Topic
4 replies Latest Post - ‏2014-07-12T18:49:00Z by gr8yash
StarSanjay
StarSanjay
1 Post
ACCEPTED ANSWER

Pinned topic Split Particular String in Java

‏2011-05-19T12:02:07Z |
This is the Actual String:

String str=P/2514863/45/25/14;

I need the following string only:

Answer:2514863

how i am gng to use regexp in it?

can u anyone provide me the solution?

Merci,

Sanjeevi M
Updated on 2012-01-29T02:56:51Z at 2012-01-29T02:56:51Z by SystemAdmin
  • chrc
    chrc
    1 Post
    ACCEPTED ANSWER

    Re: Split Particular String in Java

    ‏2011-06-09T13:14:28Z  in response to StarSanjay
    Hi,

    Can you describe how you extract 2514863 of the String ?
    Thx

    Chris
  • Gornuke
    Gornuke
    2 Posts
    ACCEPTED ANSWER

    Re: Split Particular String in Java

    ‏2011-06-25T19:27:51Z  in response to StarSanjay
    String str="P/2514863/45/25/14";
    String str2 = "Answer:"+str.substring(2, 9);
    System.out.println(str2);

    If the number has always the same length and is places in the same position. If not, you have to detect the index for the two '/' and get the substring withing them:

    String str="P/2514863/45/25/14";
    String str2 = "Answer:"+str.substring(str.indexOf('/')+1, str.indexOf('/', str.indexOf('/')+1));
    System.out.println(str2);
    • SystemAdmin
      SystemAdmin
      210 Posts
      ACCEPTED ANSWER

      Re: Split Particular String in Java

      ‏2012-01-29T02:56:51Z  in response to Gornuke
      Hi Friend,

      If you use the any of the below logics, you don't need to worry about the positions. This will work dynamically.

      Logic 1

      String str = "P/2514863/45/25/14";
      Pattern p = Pattern.compile("[/]+");
      String[] result = p.split(str);
      System.out.println("Answer : " + result[1]);

      Logic 2

      String str = "P/2514863/45/25/14";
      String[] result = str1.split("/");
      System.out.println("Answer : " + result[1]);

      I would suggest the Logic 1. Because it compiles the regex and executes it. So you would get a better performance than Logic 2.
      • gr8yash
        gr8yash
        3 Posts
        ACCEPTED ANSWER

        Re: Split Particular String in Java

        ‏2014-07-12T18:49:00Z  in response to SystemAdmin

        Java String class provide useful methods to split string using the regex. Use string.split("regex") to split string. visit string split java  for example