web-dev-qa-db-ja.com

'\ r \ n'でテキストを分割する

私はこれに従いました 記事

そして、私はこのコードを思いつきました:

string FileName = "C:\\test.txt";

using (StreamReader sr = new StreamReader(FileName, Encoding.Default))
{
    string[] stringSeparators = new string[] { "\r\n" };
    string text = sr.ReadToEnd();
    string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
    foreach (string s in lines)
    {
        Console.WriteLine(s);
    }
}

サンプルテキストを次に示します。

somet interesting text\n
some text that should be in the same line\r\n
some text should be in another line

出力は次のとおりです。

somet interesting text\r\n
some text that should be in the same line\r\n
some text should be in another line\r\n

しかし、私が欲しいのはこれです:

somet interesting textsome text that should be in the same line\r\n
some text should be in another line\r\n

私はこの結果を得るべきだと思うが、どういうわけか私は何かを見逃しています...

36
skmasq

問題は分割ではなく、WriteLineにあります。 WriteLineで印刷された文字列内の\nは、「余分な」行を生成します。

var text = 
  "somet interesting text\n" +
  "some text that should be in the same line\r\n" +
  "some text should be in another line";

string[] stringSeparators = new string[] { "\r\n" };
string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
Console.WriteLine("Nr. Of items in list: " + lines.Length); // 2 lines
foreach (string s in lines)
{
   Console.WriteLine(s); //But will print 3 lines in total.
}

問題を修正するには、文字列を印刷する前に\nを削除してください。

Console.WriteLine(s.Replace("\n", ""));
50
Magnus

これは私のために働いた。

using System.IO;

//  

    string readStr = File.ReadAllText(file.FullName);          
    string[] read = readStr.Split(new char[] {'\r','\n'},StringSplitOptions.RemoveEmptyEntries);
6

問題はテキストファイルにあると思います。おそらくすでに多すぎる行に分割されており、それを読むと、追加の\rおよび/または\n文字(ファイルに存在するもの)。 text変数に読み込まれる内容を確認してください。

以下のコード(テキストを含むローカル変数)は正常に機能し、2行に分割されます。

string[] stringSeparators = new string[] { "\r\n" };
string text = "somet interesting text\nsome text that should be in the same line\r\nsome text should be in another line";
string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
4
Szymon

テキスト領域からの入力を文字列のリストに分割するよりコンパクトなアプローチを取りました。目的に応じてこれを使用できます。

問題は、\ r\nで分割できないため、事前に\ nを削除し、\ rのみで分割することです。

var serials = model.List.Replace("\n","").Split('\r').ToList<string>();

あなたがたった1行でそれを行うことができるので、私はこのアプローチが好きです。

3
Adrian Hedley

静的Fileクラスを使用すると、ファイルの読み取りが簡単になります。

// First read all the text into a single string.
string text = File.ReadAllText(FileName);

// Then split the lines at "\r\n".   
string[] stringSeparators = new string[] { "\r\n" };
string[] lines = text.Split(stringSeparators, StringSplitOptions.None);

// Finally replace lonely '\r' and '\n' by  whitespaces in each line.
foreach (string s in lines) {
    Console.WriteLine(s.Replace('\r', ' ').Replace('\n', ' '));
}

注:テキストには、垂直タブ記号\v。これらは、Microsoft Wordで手動の改行として使用されます。

あらゆる種類のブレークをキャッチするために、置換に正規表現を使用できます

Console.WriteLine(Regex.Replace(s, @"[\f\n\r\t\v]", " "));

これは私のために働いた。

string stringSeparators = "\r\n";
string text = sr.ReadToEnd();
string lines = text.Replace(stringSeparators, "");
lines = lines.Replace("\\r\\n", "\r\n");
Console.WriteLine(lines);

最初の置換は、テキストファイルの新しい行の\r\nを置換し、2番目は、ファイルの読み取り時に\r\nに変換される実際の\\r\\nテキストを置換します。 (ファイルが読み取られると、\\\になります)。

1
davidsbro

次のコードは、意図した結果を提供します。

string text="some interesting text\nsome text that should be in the same line\r\nsome 
text should be in another line"
var results = text.Split(new[] {"\n","\r\n"}, StringSplitOptions.None);
0
user3472484