String.split() method question? - Java

This is a discussion on String.split() method question? - Java ; 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 ...

+ Reply to Thread
Results 1 to 7 of 7

String.split() method question?

  1. Default String.split() method question?

    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!

  2. Default Re: String.split() method question?

    On Dec 17, 8:59 am, "au.da...@gmail.com" <au.da...@gmail.com> 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

  3. Default Re: String.split() method question?

    "au.danji@gmail.com" <au.danji@gmail.com> 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]


  4. Default Re: String.split() method question?


    <au.danji@gmail.com> wrote in message
    news:dd28767d-ce4a-4be2-b56e-159595829782@s19g2000prg.googlegroups.com...
    > 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.



  5. Default Re: String.split() method question?

    On Dec 17, 1:41 am, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
    > "au.da...@gmail.com" <au.da...@gmail.com> 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!

  6. Default Re: String.split() method question?

    "au.danji@gmail.com" <au.danji@gmail.com> 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]


  7. Default Re: String.split() method question?

    On Sun, 16 Dec 2007 19:59:48 -0800 (PST), "au.danji@gmail.com"
    <au.danji@gmail.com> 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 to Thread

Similar Threads

  1. how to use split method in ruby
    By Application Development in forum RUBY
    Replies: 5
    Last Post: 12-07-2007, 10:37 PM
  2. How to Split a String
    By Application Development in forum Python
    Replies: 12
    Last Post: 11-30-2007, 05:04 PM
  3. Replies: 1
    Last Post: 08-27-2007, 01:31 AM
  4. IE and string.split('\n')
    By Application Development in forum Javascript
    Replies: 4
    Last Post: 05-29-2007, 11:36 AM
  5. Bizarre: string.split() method
    By Application Development in forum DOTNET
    Replies: 30
    Last Post: 06-29-2005, 11:09 AM