Split a quoted string with a delimiter

前端 未结 5 1516
清酒与你
清酒与你 2020-12-06 19:14

I want to split a string with a delimiter white space. but it should handle quoted strings intelligently. E.g. for a string like

\"John Smith\" Ted Barry 
<         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 19:28

    commons-lang has a StrTokenizer class to do this for you, and there is also java-csv library.

    Example with StrTokenizer:

    String params = "\"John Smith\" Ted Barry"
    // Initialize tokenizer with input string, delimiter character, quote character
    StrTokenizer tokenizer = new StrTokenizer(params, ' ', '"');
    for (String token : tokenizer.getTokenArray()) {
       System.out.println(token);
    }
    

    Output:

    John Smith
    Ted
    Barry
    

提交回复
热议问题