Read String line by line

前端 未结 11 1688
迷失自我
迷失自我 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:03

    Solution using Java 8 features such as Stream API and Method references

    new BufferedReader(new StringReader(myString))
            .lines().forEach(System.out::println);
    

    or

    public void someMethod(String myLongString) {
    
        new BufferedReader(new StringReader(myLongString))
                .lines().forEach(this::parseString);
    }
    
    private void parseString(String data) {
        //do something
    }
    

提交回复
热议问题