2

たとえば、Web の .txt ファイルから次の文字列をダウンロードしたとします。

line1
line2
line3

文字列全体を行ごとに分割するにはどうすればよいでしょうか? ありがとう!

使ってもいいですか?

string[] tokens = Regex.Split(input, @"\r?\n|\r");

ありがとう

4

3 に答える 3

10

すべての行File.ReadAllLinesを取得するために使用します。string[]

string[] allLines = File.ReadAllLines(path);
string line10 = allLines[9]; // exception if there are less
string line100 = allLines.ElementAtOrDefault(99); // null if there are less

使用できる文字列が既にある場合はString.SplitEnvironment.NewLine

string[] textLines = text.Split(new[]{ Environment.NewLine }, StringSplitOptions.None);
于 2013-09-13T08:32:59.267 に答える
4

これを使って:

var result = Regex.Split(text, "\r\n|\r|\n");

ここに示されているように:文字列を行に分割する最良の方法

于 2013-09-13T08:32:31.830 に答える
2

ファイルをダウンロードしている場合は、それを開いてReadAllLines

var f= File.ReadAllLines(filPath)

ReadAllLines戻りますstring[]

于 2013-09-13T08:32:51.133 に答える