Connecting Tech Pros Worldwide Help | Site Map

Splitting strings

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#1   Mar 19 '07
Breaking up a string

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

Expand|Select|Wrap|Line Numbers
  1.  String string = "This is a string"; 
  2.  
  3. String[] tokens = string.split(" ");
  4. for(String s: tokens) {
  5.     System.out.println(s);
  6. }
  7.  
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
    Expand|Select|Wrap|Line Numbers
    1. String name = "java.sql.Date";
    2. String[] s = name.split("\\.");
    3. 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
    Expand|Select|Wrap|Line Numbers
    1. String name = "java.sql.Date";
    2. String[] s = name.split("\\.", 1);
    3. System.out.println(Arrays.toString(s));
    returns the array with only one element



Newbie
 
Join Date: Nov 2006
Location: Vellore , India
Posts: 18
#2   Mar 26 '07

re: Splitting strings


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,projects still use it) will not be able to make use of it.
Regards,
-- Abdel Olakara


Quote:

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.

Expand|Select|Wrap|Line Numbers
  1.  String string = "This is a string"; 
  2.  
  3. String[] tokens = string.split(" ");
  4. for(String s: tokens) {
  5.     System.out.println(s);
  6. }
  7.  
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
    Expand|Select|Wrap|Line Numbers
    1. String name = "java.sql.Date";
    2.  
    3. String[] s = name.split("\\.");
    4.  
    5. 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
    Expand|Select|Wrap|Line Numbers
    1. String name = "java.sql.Date";
    2.  
    3. String[] s = name.split("\\.", 1);
    4.  
    5. System.out.println(Arrays.toString(s));
    returns the array with only one element

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3   Mar 26 '07

re: Splitting strings


Quote:

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,projects 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.
Newbie
 
Join Date: Aug 2008
Posts: 9
#4   Aug 27 '08

re: Splitting strings


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

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

Expand|Select|Wrap|Line Numbers
  1.  String origAVNListStr = "~`L~`L~`L~`~`~`L~`";
  2.         String[] strArr = origAVNListStr.split("~`");
  3.  
  4.          for(int i=0;i<strArr.length;i++)
  5.          {
  6.     System.out.println(i+1 +"the next token - " + strArr[i]);
  7.          }
and the output is

Expand|Select|Wrap|Line Numbers
  1. 1the next token -
  2. 2the next token - L
  3. 3the next token - L
  4. 4the next token - L
  5. 5the next token -
  6. 6the next token -
  7. 7the next token - L
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.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5   Oct 24 '08

re: Splitting strings


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

Quote:

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.

Reply