How to split a String array?

后端 未结 4 991
醉梦人生
醉梦人生 2021-02-20 00:33

Intention is to take a current line (String that contains commas), replace white space with \"\" (Trim space) and finally store split String elements into the array.

4条回答
  •  渐次进展
    2021-02-20 01:09

    If you need to perform this operation repeatedly, I'd suggest using java.util.regex.Pattern and java.util.regex.Matcher instead.

    final Pattern pattern = Pattern.compile( regex);
    for(String inp: inps) {
        final Matcher matcher = pattern.matcher( inpString);
        return matcher.replaceAll( replacementString); 
    }
    

    Compiling a regex is a costly operation and using String's replaceAll repeatedly is not recommended, since each invocation involves compilation of regex followed by replacement.

提交回复
热议问题