String.split() method question?

A

au.danji

Hello all, I have a question about the String split method.

I have a string s1:
String s1 = "a \"single unit\" test";
String[] strArr = s1.split(" ");

the result is:

strArr[0]: a
strArr[1]: "single
strArr[2]: unit"
strArr[3]: test


but I want to let "single unit" to be in the same array element, just
like this:
strArr[0]: a
strArr[1]: "single unit"
strArr[2]: test

Can anybody tell me how to do that? should I use the regular
expression?
Thanks a lot!
 
A

Abhishek

Hello all, I have a question about the String split method.

I have a string s1:
String s1 = "a \"single unit\" test";
String[] strArr = s1.split(" ");

the result is:

strArr[0]: a
strArr[1]: "single
strArr[2]: unit"
strArr[3]: test

but I want to let "single unit" to be in the same array element, just
like this:
strArr[0]: a
strArr[1]: "single unit"
strArr[2]: test

Can anybody tell me how to do that? should I use the regular
expression?
Thanks a lot!

Why don't you try concatenating the two strings after they have been
split?
once you encounter a string "single which starts with quotes, try
concatenating the following strings till you encounter something which
ends with a quote unit" .

Abhishek
 
S

Stefan Ram

String s1 = "a \"single unit\" test";
strArr[0]: a
strArr[1]: "single unit"
strArr[2]: test
Can anybody tell me how to do that?

public class Main
{ public static void main
( final java.lang.String[] args )
{ java.lang.System.out.println
( java.util.Arrays.toString
( "a \"single unit\" test".split
( "(?: (?=\"))|(?:(?<=\") )" ))); }}

[a, "single unit", test]
 
H

Hal Rosser

Hello all, I have a question about the String split method.

I have a string s1:
String s1 = "a \"single unit\" test";
String[] strArr = s1.split(" ");

the result is:

strArr[0]: a
strArr[1]: "single
strArr[2]: unit"
strArr[3]: test


but I want to let "single unit" to be in the same array element, just
like this:
strArr[0]: a
strArr[1]: "single unit"
strArr[2]: test

Can anybody tell me how to do that? should I use the regular
expression?
Thanks a lot!

If you didn't need to keep the quotes intact with the string, you could
split on the quote.
String[] strArr = s1.split("\"");

I guess you could add the quotes back.
strArr[1] = "\"" + strArr[1] + "\"";
.... You could just get the array length and add the quotes to all elements
that do not begin and end with a space.
Since you split the string on the quote marks, the parts of the string that
had quotes around it will probably begin with a regular word character, and
not a space.
 
A

au.danji

String s1 = "a \"single unit\" test";
strArr[0]: a
strArr[1]: "single unit"
strArr[2]: test
Can anybody tell me how to do that?

public class Main
{ public static void main
( final java.lang.String[] args )
{ java.lang.System.out.println
( java.util.Arrays.toString
( "a \"single unit\" test".split
( "(?: (?=\"))|(?:(?<=\") )" ))); }}

[a, "single unit", test]



Hi, thanks a lot for your help, if I need to split a string like this:
"this is a \"single unit\" test another "second unit" one"
is it possible to get results:

a[0] = this
a[1] = is
a[2] = a
a[3] = single unit
a[4] = test
a[5] = another
a[6] = second unit
a[7] = one

Most appreciate!
 
S

Stefan Ram

"this is a \"single unit\" test another "second unit" one"
is it possible to get results:
a[0] = this
a[1] = is
a[2] = a
a[3] = single unit
a[4] = test
a[5] = another
a[6] = second unit
a[7] = one

public class Main
{ public static void main
( final java.lang.String[] args )
{ java.lang.System.out.println
( java.util.Arrays.toString
( "this is a \"single unit\" test another \"second unit\" one".split
( "(?<!\"\\w{1,32}) (?!\\w+\")" ))); }}

[this, is, a, "single unit", test, another, "second unit", one]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top