473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Want to split string on 1 or more white spaces

I have the following:

String[] myStringArray;
String myString = "98 99 100";

I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it will split myString up using the delimiter of 1 space
so that
myStringArray[0] = 98
myStringArray[1] = 99
myStringArray[2] = 100

How do I write my split code so that it will split myString
on 1 or more spaces???

In other words, the following 2 strings should split up the
same so that the 2 spaces between the 88 and 89 are
handled.

"88 89 90"
"88 89 90"

myStringArray[0] = 88
myStringArray[1] = 89
myStringArray[2] = 90
TIA
Jul 17 '05 #1
5 31150
Split works on reular expressions so use the right regexp instead of " "

Silvio Bierman
"Stu Cazzo" <ff***@yahoo.com> wrote in message
news:61**************************@posting.google.c om...
I have the following:

String[] myStringArray;
String myString = "98 99 100";

I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it will split myString up using the delimiter of 1 space
so that
myStringArray[0] = 98
myStringArray[1] = 99
myStringArray[2] = 100

How do I write my split code so that it will split myString
on 1 or more spaces???

In other words, the following 2 strings should split up the
same so that the 2 spaces between the 88 and 89 are
handled.

"88 89 90"
"88 89 90"

myStringArray[0] = 88
myStringArray[1] = 89
myStringArray[2] = 90
TIA

Jul 17 '05 #2
ff***@yahoo.com (Stu Cazzo) wrote in message news:<61**************************@posting.google. com>...
I have the following:

String[] myStringArray;
String myString = "98 99 100";

I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it will split myString up using the delimiter of 1 space
so that
myStringArray[0] = 98
myStringArray[1] = 99
myStringArray[2] = 100

How do I write my split code so that it will split myString
on 1 or more spaces???

In other words, the following 2 strings should split up the
same so that the 2 spaces between the 88 and 89 are
handled.

"88 89 90"
"88 89 90"

myStringArray[0] = 88
myStringArray[1] = 89
myStringArray[2] = 90
TIA

myStringArray = myString.split("\\s+");
Jul 17 '05 #3
Ditto, don't re-invent the wheel if you don't have to.
"hiwa" <HG******@nifty.ne.jp> wrote in message
news:68**************************@posting.google.c om...
ff***@yahoo.com (Stu Cazzo) wrote in message

news:<61**************************@posting.google. com>...
I have the following:

String[] myStringArray;
String myString = "98 99 100";

I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it will split myString up using the delimiter of 1 space
so that
myStringArray[0] = 98
myStringArray[1] = 99
myStringArray[2] = 100

How do I write my split code so that it will split myString
on 1 or more spaces???

In other words, the following 2 strings should split up the
same so that the 2 spaces between the 88 and 89 are
handled.

"88 89 90"
"88 89 90"

myStringArray[0] = 88
myStringArray[1] = 89
myStringArray[2] = 90
TIA

myStringArray = myString.split("\\s+");

Jul 17 '05 #4

"Stu Cazzo" <ff***@yahoo.com> wrote in message
news:61**************************@posting.google.c om...
I have the following:

String[] myStringArray;
String myString = "98 99 100";

I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it will split myString up using the delimiter of 1 space
so that
myStringArray[0] = 98
myStringArray[1] = 99
myStringArray[2] = 100

How do I write my split code so that it will split myString
on 1 or more spaces???

In other words, the following 2 strings should split up the
same so that the 2 spaces between the 88 and 89 are
handled.

"88 89 90"
"88 89 90"

myStringArray[0] = 88
myStringArray[1] = 89
myStringArray[2] = 90


A suitable solution has already been posted - using 'split' with an RE [in
this case, it would probably be "\x20+" using a greedy quantifier to 'gobble
up' all the space characters].

I'd like to offer an alternative solution, one that might be useful to those
using a pre-1.4 J2SDK [besides, I always enjoy writing string
manipulation-related code ;) !]. The code consists of:

* A 'split' method that mimics 1.4's 'String.split' except
for the RE-expansion component

* A 'squeeze' method which reduces all multiple sequential
occurrences of a specifed character

To effect a split on the space character ensuring multiple sequential
occurrences have been removed you would do:

String string = "...";
String[] array = Str.split(Str.squeeze(string, ' '), " ");

Please note this is offered more as a learning exercise than a practical
solution, though it can be the latter in certain cases.

Cheers,

Anthony Borla

// ===================================
import java.util.*;

public class TestStr
{
public static void main(String[] args)
{

// *** Testing 'squeeze'

// String s1 = Str.squeeze("ABC", '|', true);
// String s2 = Str.squeeze("", '|', true);
// String s3 = Str.squeeze("|", '|', true);
// String s4 = Str.squeeze("||||", '|', true);
// String s5 = Str.squeeze("ABC|def", '|', true);
// String s6 = Str.squeeze("ABC||def|", '|', true);
// String s7 = Str.squeeze("ABC||def|||", '|', true);
// String s8 = Str.squeeze("ABC||| |def||||| |||", ' ', true);
// String s9 = Str.squeeze("ABC||| |def||||| |||", '|', true);
// System.out.println(s1);
// System.out.println(s2);
// System.out.println(s3);
// System.out.println(s4);
// System.out.println(s5);
// System.out.println(s6);
// System.out.println(s7);
// System.out.println(s8);
// System.out.println(s9);

// *** Testing 'split'

// String[] s1 = Str.split("asv rrr 345 123 nmj", " ", true);
// String[] s2 = Str.split("asv rrr 345 123 nmj ", " ", true);
// String[] s3 = Str.split("asv rrr 345 123 nmj ", " ", true);
// String[] s4 = Str.split("asvrrr345123nmj", " ", true);

// *** Testing 'squeeze' and 'split' together

String unsqueezed = "1|||2||3|4||5|||6|7||8";

String[] numbers1 = Str.split(unsqueezed, "|", false);
for (int i = 0; i < numbers1.length; ++i)
System.out.println(numbers1[i]);

String numberStr = Str.squeeze(unsqueezed, '|', false);
String[] numbers2 = Str.split(numberStr, "|", false);
for (int i = 0; i < numbers2.length; ++i)
System.out.println(numbers2[i]);
}
}

class Str
{
public static String squeeze(String source, char squeezeChar, boolean
debug)
{
StringBuffer sb = new StringBuffer(source);
String squeezeStr = Character.toString(squeezeChar);

int spos = 0;

while (!((spos = sb.indexOf(squeezeStr, spos)) < 0))
while (++spos < sb.length() && sb.charAt(spos) == squeezeChar)
{
// *** Removable code
if (debug)
sb.setCharAt(spos, '#');
else
sb.deleteCharAt(spos--);
}

return sb.toString();
}

public static String[] split(String source, String pattern, boolean debug)

{
ArrayList list = new ArrayList();

int spos = 0, epos = 0, patternLength = pattern.length();

if (source.indexOf(pattern) < 0)
{
list.add(source);
}
else
{
String item = new String();

do
{
if ((epos = source.indexOf(pattern, spos)) < 0)
{
item = source.substring(spos, source.length());

// *** Removable Code
if (debug)
{
System.out.println("Item = " + item + " length: " +
item.length());
System.out.println("Pattern= " + pattern + " length: " +
pattern.length());
System.out.println("S: " + spos + " E: " + epos + " ==> " +
item);
}

if (item.length() > 0 && item.compareTo(pattern) != 0)
list.add(item);

break;
}

item = source.substring(spos, epos);

// *** Removable Code
if (debug)
{
System.out.println("S: " + spos + " E: " + epos + " ==> " + item);
}

if (item.length() > 0 && item.compareTo(pattern) != 0)
list.add(item);

spos = epos + patternLength;

} while (epos <= spos);
}

// *** Removable Code
if (debug)
{
System.out.println("List Size: " + list.size());
}

return ((String[]) list.toArray(new String[list.size()]));
}
}

Jul 17 '05 #5
If not using regex, see Tool #2: String manipulation split() method in

http://www.javaworld.com/javaworld/j...ltools-p2.html

"Stu Cazzo" <ff***@yahoo.com> wrote in message
news:61**************************@posting.google.c om...
I have the following:

String[] myStringArray;
String myString = "98 99 100";

I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it will split myString up using the delimiter of 1 space
so that
myStringArray[0] = 98
myStringArray[1] = 99
myStringArray[2] = 100

How do I write my split code so that it will split myString
on 1 or more spaces???

In other words, the following 2 strings should split up the
same so that the 2 spaces between the 88 and 89 are
handled.

"88 89 90"
"88 89 90"

myStringArray[0] = 88
myStringArray[1] = 89
myStringArray[2] = 90
TIA

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: qwweeeit | last post by:
Hi all, I need to limit as much as possible the lenght of a source line, stripping white spaces (except indentation). For example: .. . max_move and AC_RowStack.acceptsCards ( self,...
4
by: ucfcpegirl06 | last post by:
Hi, I need help getting rid of trailing white spaces. I am searching a file for various data (not important) and retrieving it. I output the data if found to a file. An example would be:...
5
by: Jonathan Ng | last post by:
Hi, I was wondering if there was a way to include the white spaces in a string. Currently, I am using: scanf("%s", &input); However, this doesn't include the 'space' character or any other...
11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
3
by: Prince | last post by:
I have some <RequiredFieldValidator> on my page and everything works fine except that there are lots of white spaces between the web server controls that are being validated. I've set the Display...
3
by: Sergey | last post by:
Which module to use to do such thing: "-a -b -c '1 2 3'" -> (i have string, need to pass it to getopt)
3
by: ayan4u | last post by:
well i need to deal with white spaces in charecter arrays... with static arrays its fine.. char ss; cin.getline(ss, sizeof ss); .... //deals with white spaces
3
by: rupinderbatra | last post by:
Hello everyone, I am using a regular expression to parse a text string into various parts -- for ex: string "How do you do" will be changed to array with all the words and white spaces. I am...
3
by: HaifaCarina | last post by:
I want to save and print a full name in one array. The program would look like this: Enter your full name: Mary Smith Your full name is Mary Smith. but I don't know how. what i did was i...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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...
0
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...

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.