How to split a comma separated String while ignoring escaped commas?

后端 未结 4 1360
我寻月下人不归
我寻月下人不归 2020-11-29 03:43

I need to write a extended version of the StringUtils.commaDelimitedListToStringArray function which gets an additional parameter: the escape char.

so calling my:

4条回答
  •  -上瘾入骨i
    2020-11-29 04:08

    For future reference, here is the complete method i ended up with:

    public static String[] commaDelimitedListToStringArray(String str, String escapeChar) {
        // these characters need to be escaped in a regular expression
        String regularExpressionSpecialChars = "/.*+?|()[]{}\\";
    
        String escapedEscapeChar = escapeChar;
    
        // if the escape char for our comma separated list needs to be escaped 
        // for the regular expression, escape it using the \ char
        if(regularExpressionSpecialChars.indexOf(escapeChar) != -1) 
            escapedEscapeChar = "\\" + escapeChar;
    
        // see http://stackoverflow.com/questions/820172/how-to-split-a-comma-separated-string-while-ignoring-escaped-commas
        String[] temp = str.split("(?

提交回复
热议问题