3

The code I used to split the string with any number of white space is

String out="SELECT * FROM EMP WHERE EMPID=10";
String array[] = out.split("\\s+");

I want to include the white spaces in the "out" string into the array[] object while splitting with white spaces. The output I want is:

array[]={"SELECT","[WHITE SPACE]","*","[WHITE SPACE]","FROM","[WHITE SPACE]","EMP","[WHITE SPACE]","WHERE","[WHITE SPACE]","EMPID=10"}

But the output what I am getting is:

 array[]={"SELECT","*","FROM","EMP","WHERE","EMPID=10"}

Any help will be appreciated

zameer
  • 471
  • 1
  • 5
  • 15
  • Possible duplicate of [How to split a string, but also keep the delimiters?](http://stackoverflow.com/questions/2206378/how-to-split-a-string-but-also-keep-the-delimiters) – ashiquzzaman33 Dec 10 '15 at 07:57

3 Answers3

5

Since split accepts a regex, you can use lookarounds:

out.split("(?<=\\s)|(?=\\s)");

This will output:

[SELECT,  , *,  , FROM,  , EMP,  , WHERE,  , EMPID=10]

Note that the delimiter (space in your case) is included in the result.

Maroun
  • 94,125
  • 30
  • 188
  • 241
3

Try to split it like this:

  String array[] = out.split("(?=\\s)",-1);

The code is:

 public static void main(String[] args) {
    String out="SELECT * FROM EMP WHERE EMPID=10";
    String array[] = out.split("(?=\\s)",-1);

    for (String string : array) {       
    System.out.print(string);
    }
  }

Output:

SELECT * FROM EMP WHERE EMPID=10
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • if i want to split with ; and include the semicolon in the array list, can i use String array[] = out.split("(?=\;)",-1) – zameer Dec 12 '15 at 09:41
  • @zameer can you put me some input and expect output – Abdelhak Dec 12 '15 at 09:46
  • I mean to say, How you split the string by referring white space with the help of out.split("(?=\\s)",-1).Now my question is how can i split the string with the help of semicolon (;). Can i use the above solution like String array[] = out.split("(?=\\;)",-1). – zameer Dec 12 '15 at 10:25
  • the above comment didn't fulfill by requirement, I am pasting the code.My requirement is i want to add the semicolon to the string when ever it is splitting the string but the semicolon is adding to next string ,here is the code String sampleContent="hello ; hai ; come fast ;"; String SQLScripts[] = sampleContent.split("(?=\\;)",-1); after looping in the for loop i am getting the out put as===>: OUTPUT: hello ,OUTPUT: ; hai ,OUTPUT: ; come fast ,OUTPUT: ; But i want semicolon has to be added to the first splitting string only Like : hello; hai; – zameer Dec 16 '15 at 06:00
  • @zameer I used this:sampleContent.split("(?=\\s)",-1); and I hade this output : hello ; hai ; come fast ;===> if you want just : hello ; hai ; give some time to do it – Abdelhak Dec 16 '15 at 07:04
  • thank for your replay ,i am actually splitting wit semicolon not with the white space .i think you splitting with white space .String SQLScripts[] = sampleContent.split("(?=\\;)",-1); – zameer Dec 16 '15 at 07:09
2

Try this one,

String out="SELECT * FROM EMP WHERE EMPID=10";
    out = out.replaceAll("\\s+", ",[WHITE SPACE],");

    String [] array = out.split(",");

    ArrayList<String> list = new ArrayList<String>();

    for(String str : array){
        list.add(str);
    }
    System.out.println(list);

output : enter image description here

Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55