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

前端 未结 10 1042
醉酒成梦
醉酒成梦 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:22

    You could use String.indexOf()/String.substring()

    String separator = System.getProperty("line.separator");
    int index = textContent.indexOf(separator);
    
    while (index > 0)
    {
      int nextIndex = textContent.indexOf(separator, index + separator.length());
      String line = textContent.substring(index + separator.length(), nextIndex);
    
      // do something with line.
    }
    

提交回复
热议问题