473,233 Members | 1,673 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,233 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 31132
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.