How to split string with trailing empty strings in result?

后端 未结 2 942
一整个雨季
一整个雨季 2020-12-10 10:58

I am a bit confused about Scala string split behaviour as it does not work consistently and some list elements are missing. For example, if I have a CSV string with 4 column

2条回答
  •  失恋的感觉
    2020-12-10 11:05

    As Peter mentioned in his answer, "string".split(), in both Java and Scala, does not return trailing empty strings by default.

    You can, however, specify for it to return trailing empty strings by passing in a second parameter, like this:

    String s = "elem1,elem2,,";
    String[] tokens = s.split(",", -1);
    

    And that will get you the expected result.

    You can find the related Java doc here.

提交回复
热议问题