How to split a String array?

后端 未结 4 988
醉梦人生
醉梦人生 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:02

    What you wrote does not match the code:

    Intention is to take a current line which contains commas, store trimmed values of all space and store the line into the array.

    It seams, by the code, that you want all spaces removed and split the resulting string at the commas (not described). That can be done as Paul Tomblin suggested.

    String[] currentLineArray = currentInputLine.replaceAll("\\s", "").split(",");
    

    If you want to split at the commas and remove leading and trailing spaces (trim) from the resulting parts, use:

    String[] currentLineArray = currentInputLine.trim().split("\\s*,\\s*");
    

    (trim() is needed to remove leading spaces of first part and trailing space from last part)

提交回复
热议问题