473,472 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Splitting strings

13,262 MVP
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
Mar 19 '07 #1
4 21659
olakara
18 New Member
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


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
Mar 26 '07 #2
r035198x
13,262 MVP
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.
Mar 26 '07 #3
giffy
9 New Member
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.
Aug 27 '08 #4
r035198x
13,262 MVP
It's not really a failure but is the behavior clearly documented in the specs for that method.

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.
Oct 24 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Steven Bethard | last post by:
Here's what I'm doing: >>> lst = >>> splits = >>> for s in lst: .... pair = s.split(':') .... if len(pair) != 2: .... pair.append(None) .... splits.append(pair) ....
7
by: Jeremy Sanders | last post by:
I have a large string containing lines of text separated by '\n'. I'm currently using text.splitlines(True) to break the text into lines, and I'm iterating over the resulting list. This is very...
4
by: sbucking | last post by:
im trying to split a string with this form (the string is from a japanese dictionary file with mulitple definitions in english for each japanese word) str1 / (def1, ...) (1) def2 / def3 /...
3
by: Aaron Walker | last post by:
I have a feeling this going to end up being something so stupid, but right now I'm confused as hell. I'm trying to code a function, that given a string and a delimiter char, returns a vector of...
2
by: John Perks and Sarah Mount | last post by:
I have to split some identifiers that are casedLikeThis into their component words. In this instance I can safely use to represent uppercase, but what pattern should I use if I wanted it to work...
1
by: madrobmegee | last post by:
I am wondering if any one knows how to split/parse a string of text up at special defined characters, such as a comma. As I need to split it into arrays to be displayed on a form.
6
by: chezz | last post by:
Hey i looked at some earlier discussions on this topic and they seem a bit complex i found this code example on another site - char str = "200.0.0.90-200.0.0.223"; char *first =...
4
by: pkj7461 | last post by:
I want to split a string based on a pattern. my string is something like this. 10/8/2007 5:03:06 PM thakurab *************RECIVED MAIL FROM MIKE FOR THE PQR AND DPS...
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i have the following string value: xx__field_name__0__0 is it possible to split the string using the double underscores? thanks, rodchar
4
by: Eyes Of Madness | last post by:
I'm doing a program for a class of mine and I am having trouble splitting my strings up. I know you can do something like: a = '012345' a returns 012 but I am inputing strings of varying...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.