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

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

    use BufferedReader with StringReader argument. BufferedReader has a method readLine() so you can read your string line by line.

        StringReader reader = new StringReader(myBigTextString);
        BufferedReader br = new BufferedReader(reader);
        String line;
        while((line=br.readLine())!=null)
        {
            //do what you want
        }
    

提交回复
热议问题