本文介绍了逐行读取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个不太长的字符串,逐行读取它的最佳方法是什么?

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

我知道你可以这样做:

BufferedReader reader = new BufferedReader(new StringReader(<string>));
reader.readLine();

另一种方法是在eol上获取子字符串:

Another way would be to take the substring on the eol:

final String eol = System.getProperty("line.separator");
output = output.substring(output.indexOf(eol + 1));

还有其他更简单的方法吗?我对上述方法没有任何问题,只是想知道你们中是否有人知道看起来更简单,更有效的东西吗?

Any other maybe simpler ways of doing it? I have no problems with the above approaches, just interested to know if any of you know something that may look simpler and more efficient?

推荐答案

你也可以使用String的 split 方法:

You can also use the split method of String:

String[] lines = myString.split(System.getProperty("line.separator"));

这为您提供了方便数组中的所有行。

This gives you all lines in a handy array.

我不知道拆分的性能。它使用正则表达式。

I don't know about the performance of split. It uses regular expressions.

这篇关于逐行读取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:08