Connecting Tech Pros Worldwide Help | Site Map

Splitting strings

  #1  
Old March 19th, 2007, 10:54 AM
Administrator
 
Join Date: Sep 2006
Posts: 12,084
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



  #2  
Old March 26th, 2007, 09:53 AM
Newbie
 
Join Date: Nov 2006
Location: Vellore , India
Posts: 18

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
  #3  
Old March 26th, 2007, 12:31 PM
Administrator
 
Join Date: Sep 2006
Posts: 12,084

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.
  #4  
Old August 27th, 2008, 09:23 PM
Newbie
 
Join Date: Aug 2008
Posts: 9

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.
  #5  
Old October 24th, 2008, 08:50 AM
Administrator
 
Join Date: Sep 2006
Posts: 12,084

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting strings Eyes Of Madness answers 4 April 10th, 2008 12:12 AM
trouble splitting strings Aaron Walker answers 3 July 23rd, 2005 02:10 AM
splitting strings with python sbucking@gmail.com answers 4 July 19th, 2005 02:57 AM
Splitting strings - by iterators? Jeremy Sanders answers 7 July 18th, 2005 11:01 PM
list comprehension for splitting strings into pairs Steven Bethard answers 5 July 18th, 2005 05:31 PM