Read String line by line

前端 未结 11 1687
迷失自我
迷失自我 2020-11-29 16:38

Given a string that isn\'t too long, what is the best way to read it line by line?

I know you can do:

BufferedReader reader = new BufferedReader(new          


        
11条回答
  •  一整个雨季
    2020-11-29 17:22

    There is also Scanner. You can use it just like the BufferedReader:

    Scanner scanner = new Scanner(myString);
    while (scanner.hasNextLine()) {
      String line = scanner.nextLine();
      // process the line
    }
    scanner.close();
    

    I think that this is a bit cleaner approach that both of the suggested ones.

提交回复
热议问题