Split a quoted string with a delimiter

前端 未结 5 1514
清酒与你
清酒与你 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:42

    well, i made a small snipet that does what you want and some more things. since you did not specify more conditions i did not go through the trouble. i know this is a dirty way and you can probably get better results with something that is already made. but for the fun of programming here is the example:

        String example = "hello\"John Smith\" Ted Barry lol\"Basi German\"hello";
        int wordQuoteStartIndex=0;
        int wordQuoteEndIndex=0;
    
        int wordSpaceStartIndex = 0;
        int wordSpaceEndIndex = 0;
    
        boolean foundQuote = false;
        for(int index=0;index

    this also checks for words that were not separated with a space after or before the quotes, such as the words "hello" before "John Smith" and after "Basi German".

    when the string is modified to "John Smith" Ted Barry the output is three strings, 1) "John Smith" 2) Ted 3) Barry

    The string in the example is hello"John Smith" Ted Barry lol"Basi German"hello and prints 1)hello 2)"John Smith" 3)Ted 4)Barry 5)lol 6)"Basi German" 7)hello

    Hope it helps

提交回复
热议问题