Search This Blog

Showing posts with label Splitting at space if not between quotes. Show all posts
Showing posts with label Splitting at space if not between quotes. Show all posts

Thursday, 18 February 2016

Split String at Space ,Except if it is not in double qoutes


Split String at Space ,Except if it is not in double qoutes


 public class Main {
    public static void main(String[] args) {
        String str= "abc \"peter micheal\" Jean \"Patrick David\" Richards";
        String[] tempArr = str.split("[ ]+(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
       for(String string : tempArr) {
         System.out.println(string);
      }
  }
}

You can use online java compiler such as THIS to verify usability.
This will help you to avoid chances of using wrong solution and save your time.

Output: 

abc
"peter micheal"
Jean
"Patrick David"
Richards