Substring

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haran22
    New Member
    • May 2007
    • 11

    #1

    Substring

    hi to all. I have a problem i finding substring.
    i have many strings in array
    one string as the value String s1="a|b"
    2nd as the value String s2= "ab|cd"
    3rd as the value String s3= "a|cde"

    i want to extract or substring these string values as two strings which is seprated by '|'.
    for eg 1st is substring as a and b
    2nd is ab and cd
    3rd is a and cde.

    i know only this way to find
    s1.substring(0, 1);
    s1.substring(2, 3);
    insteading of giving starting and ending position it want to take upto that symbol '|'
  • rsrinivasan
    New Member
    • Mar 2007
    • 221

    #2
    Originally posted by haran22
    hi to all. I have a problem i finding substring.
    i have many strings in array
    one string as the value String s1="a|b"
    2nd as the value String s2= "ab|cd"
    3rd as the value String s3= "a|cde"

    i want to extract or substring these string values as two strings which is seprated by '|'.
    for eg 1st is substring as a and b
    2nd is ab and cd
    3rd is a and cde.

    i know only this way to find
    s1.substring(0, 1);
    s1.substring(2, 3);
    insteading of giving starting and ending position it want to take upto that symbol '|'
    Hi,

    Use StringTokenizer Class to get sub string. If you want code i'll send to you

    Thanks,
    Srinivas r.

    Comment

    • rsrinivasan
      New Member
      • Mar 2007
      • 221

      #3
      Originally posted by haran22
      hi to all. I have a problem i finding substring.
      i have many strings in array
      one string as the value String s1="a|b"
      2nd as the value String s2= "ab|cd"
      3rd as the value String s3= "a|cde"

      i want to extract or substring these string values as two strings which is seprated by '|'.
      for eg 1st is substring as a and b
      2nd is ab and cd
      3rd is a and cde.

      i know only this way to find
      s1.substring(0, 1);
      s1.substring(2, 3);
      insteading of giving starting and ending position it want to take upto that symbol '|'
      Hi,
      This is the code to get substring

      Code:
                      String str = "ab|cd";
      	StringTokenizer st = new StringTokenizer(str , "|");
      	while (st.nextElements())
      	    System.out.println(st.nextToken());
      Thanks,

      Srinivas r.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        ... or simply use the String.split("\ \|") method.

        kind regards,

        Jos

        Comment

        Working...