Splitting strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    Splitting strings

    Breaking up a string

    Instead of using the old StringTokenizer class, a simple trick is to use the String.split method.

    Code:
     String string = "This is a string"; 
     
    String[] tokens = string.split(" ");
    for(String s: tokens) {
    	System.out.println(s);
    }
    prints

    This
    is
    a
    string

    The split method returns an array of tokens and takes a string that represents regular expression as argument. In the above example I gave " " (space) as the argument so the string was split on space.

    Note
    • If the expression does not match any part of the input then the resulting array has just one element, namely this string.
    • to split on special characters you need to escape them using the \ character e.g
      Code:
      String name = "java.sql.Date";
      String[] s = name.split("\\.");
      System.out.println(Arrays.toString(s));
    • There are actually two split methods in the String class. The second one takes a regular expression and an integer representing the limit e.g
      Code:
      String name = "java.sql.Date";
      String[] s = name.split("\\.", 1);
      System.out.println(Arrays.toString(s));
      returns the array with only one element
  • olakara
    New Member
    • Nov 2006
    • 18

    #2
    hi,
    Its also important to note that split method will work only from JDK1.5. Those who work with JDK 1.4 (In industry many product,project s still use it) will not be able to make use of it.
    Regards,
    -- Abdel Olakara


    Originally posted by r035198x
    Breaking up a string

    Instead of using the old StringTokenizer class, a simple trick is to use the String.split method.

    Code:
     String string = "This is a string"; 
     
    String[] tokens = string.split(" ");
    for(String s: tokens) {
    	System.out.println(s);
    }
    prints

    This
    is
    a
    string

    The split method returns an array of tokens and takes a string that represents regular expression as argument. In the above example I gave " " (space) as the argument so the string was split on space.

    Note
    • If the expression does not match any part of the input then the resulting array has just one element, namely this string.
    • to split on special characters you need to use the \ character e.g
      Code:
      String name = "java.sql.Date";
       
      String[] s = name.split("\\.");
       
      System.out.println(Arrays.toString(s));
    • There are actually two split methods in the String class. The second one takes a regular expression an integer representing the limit e.g
      Code:
      String name = "java.sql.Date";
       
      String[] s = name.split("\\.", 1);
       
      System.out.println(Arrays.toString(s));
      returns the array with only one element

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by olakara
      hi,
      Its also important to note that split method will work only from JDK1.5. Those who work with JDK 1.4 (In industry many product,project s still use it) will not be able to make use of it.
      Regards,
      -- Abdel Olakara
      Not really. 1.4 supports the String.split method. You can see the API for 1.4 here.

      Comment

      • giffy
        New Member
        • Aug 2008
        • 9

        #4
        HOW to get the spaces out of a delimited string ??

        here is a usage scenario where split() seems not to work

        Code:
         String origAVNListStr = "~`L~`L~`L~`~`~`L~`";
                String[] strArr = origAVNListStr.split("~`");
                
                 for(int i=0;i<strArr.length;i++)
                 {
        	System.out.println(i+1 +"the next token - " + strArr[i]);
                 }
        and the output is

        Code:
        [INDENT]1the next token -
        2the next token - L
        3the next token - L
        4the next token - L
        5the next token -
        6the next token -
        7the next token - L[/INDENT]
        So as we can see - that the last space that shall be displayed as

        "8the next token" is not displayed.

        As far as this space reading from the string in an Array is concerned StringTokenizer fails even miserably. It will not take any of the spaces(either at the start/end or in between)

        Is there any way out of it. Sincere thanks in advance.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          It's not really a failure but is the behavior clearly documented in the specs for that method.

          Originally posted by API specs
          This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

          Comment

          Working...