I was following this article
And I came up with this code:
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);
}
}
Here is the sample text:
somet interesting text\n
some text that should be in the same line\r\n
some text should be in another line
Here is the output:
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
But what I want is this:
somet interesting textsome text that should be in the same line\r\n
some text should be in another line\r\n
I think I should get this result but somehow I am missing something...
Following code gives intended results.
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);
I think the problem is in your text file. It's probably already split into too many lines and when you read it, it "adds" additional \r
and/or \n
characters (as they exist in file). Check your what is read into text
variable.
The code below (on a local variable with your text) works fine and splits into 2 lines:
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);
Reading the file is easier done with the static File
class:
// 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', ' '));
}
Note: The text might also contain vertical tabulators \v
. Those are used by Microsoft Word as manual linebreaks.
In order to catch any possible kind of breaks, you could use regex for the replacement
Console.WriteLine(Regex.Replace(s, @"[\f\n\r\t\v]", " "));
This worked for me.
string stringSeparators = "\r\n";
string text = sr.ReadToEnd();
string lines = text.Replace(stringSeparators, "");
lines = lines.Replace("\\r\\n", "\r\n");
Console.WriteLine(lines);
The first replace replaces the \r\n
from the text file's new lines, and the second replaces the actual \r\n
text that is converted to \\r\\n
when the files is read. (When the file is read \
becomes \\
).
I took a more compact approach to split an input resulting from a text area into a list of string . You can use this if suits your purpose.
the problem is you cannot split by \r\n so i removed the \n beforehand and split only by \r
var serials = model.List.Replace("\n","").Split('\r').ToList<string>();
I like this approach because you can do it in just one line.
This worked for me.
using System.IO;
//
string readStr = File.ReadAllText(file.FullName);
string[] read = readStr.Split(new char[] {'\r','\n'},StringSplitOptions.RemoveEmptyEntries);
In Winform App(C#):
static string strFilesLoc = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), @"..\..\")) + "Resources\\";
public static string[] GetFontFamily()
{
var result = File.ReadAllText(strFilesLoc + "FontFamily.txt").Trim();
string[] items = result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
return items;
}
In-text file(FontFamily.txt):
Microsoft Sans Serif
9
true
Source: Stackoverflow.com