What is the best way to iterate over the lines of a Java String?

前端 未结 10 1051
醉酒成梦
醉酒成梦 2020-12-25 09:45

Currently I\'m using something like :

String[]lines = textContent.split(System.getProperty(\"line.separator\"));
for(String tmpLine : lines){
   //do somethi         


        
10条回答
  •  无人及你
    2020-12-25 10:21

    Guava's Splitter works well. Especially as you can remove blank lines

    Splitter splitter = Splitter.on(System.getProperty("line.separator"))
                                .trimResults()
                                .omitEmptyStrings();
    for (String line : splitter.split(input)){
       // do work here
    }
    

提交回复
热议问题