Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String.split() method question?

Reply
Thread Tools

String.split() method question?

 
 
au.danji@gmail.com
Guest
Posts: n/a
 
      12-17-2007
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!
 
Reply With Quote
 
 
 
 
Abhishek
Guest
Posts: n/a
 
      12-17-2007
On Dec 17, 8:59 am, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      12-17-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> 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]

 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      12-17-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
au.danji@gmail.com
Guest
Posts: n/a
 
      12-17-2007
On Dec 17, 1:41 am, (E-Mail Removed)-berlin.de (Stefan Ram) wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
> > 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!
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      12-17-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:
>"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]

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-18-2007
On Sun, 16 Dec 2007 19:59:48 -0800 (PST), "(E-Mail Removed)"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>Can anybody tell me how to do that? should I use the regular
>expression?


Anything to do with balancing delimiters tends to overamp a regex. You
will have an easier time with a miniature parser instead, perhaps one
you write with indexOf and charAt.

see http://mindprod.com/jgloss/parser.html
http://mindprod.com/jgloss/regex.html

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
invoke a method by reflection£¨the method's parameters can not be ArrayList? jerry051 ASP .Net 2 08-02-2005 10:35 AM
BC30289: Statement cannot appear within a method body. End of method assumed. Carlos Oliveira ASP .Net 0 08-19-2004 07:51 PM
Difference between Delete method and RemoveRow method CW ASP .Net 0 04-01-2004 01:07 AM
ASP.NET: BC30289: Statement cannot appear within a method body. End of method assumed. Mike Wilmot ASP .Net 0 12-15-2003 07:49 PM



Advertisments