Split lines into many lines with a new line?

I have input that needs to be split into multiple values โ€‹โ€‹... i.e.

2345 \ n564532 \ n345634 \ n234 234543 \ n1324 2435 \ n

The length is incompatible when I receive it, the interval is incompatible when it is present, and I want to analyze the last 3 digits before each \ n. How do I break a line and turn it into a new line? as I said, in this round it can have 3 \ n commands, next time it can be 10, how can I create 3 new lines, analyze them, and then destroy them until the next 10 appear?

string[] result = x.Split('\r'); result = x.Split(splitAtReturn, StringSplitOptions.None); string stringToAnalyze = null; foreach (string s in result) { if (s != "\r") { stringToAnalyze += s; } else { how do i analyze the characters here? } } 
+7
string c # newline string-split
source share
4 answers

You can use string.Split method. In particular, I suggest using overloading, which uses a string array of possible delimiters. This is because splitting a newline is a unique problem. In your example, all newline characters are just "\ n", but for some OSs, the new char string is "\ r \ n", and if you cannot rule out the possibility of having deuces in the same file,

 string test = "2345\n564532\n345634\n234 234543\n1324 2435\n"; string[] result = test.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries); 

Instead, if you are sure that the file contains only the newline separator allowed by your OS, you can use

 string test = "2345\n564532\n345634\n234 234543\n1324 2435\n"; string[] result = test.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries); 

StringSplitOptions.RemoveEmptyEntries allows you to commit a pair of consecutive newlines or a trailing newline as an empty string.

Now you can work with the array, studying the last 3 digits of each line

 foreach(string s in result) { // Check to have at least 3 chars, no less // otherwise an exception will occur int maxLen = Math.Min(s.Length, 3); string lastThree = s.Substring(s.Length - maxLen, maxLen); ... work on last 3 digits } 

Instead, if you want to work only using the newline character index without splitting the original string, you can use string.IndexOf this way

 string test = "2345\n564532\n345634\n234 234543\n1324 2435\n"; int pos = -1; while((pos = test.IndexOf('\n', pos + 1)) != -1) { if(pos < test.Length) { string last3part = test.Substring(pos - 3, 3); Console.WriteLine(last3part); } } 
+21
source share
 string lines = "2345\n564532\n345634\n234 234543\n1324 2435\n"; var last3Digits = lines.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Select(line => line.Substring(line.Length - 3)) .ToList(); foreach(var my3digitnum in last3Chars) { } 

last3Digits: [345, 532, 634, 543, 435]

+2
source share

This answer has been answered, check this thread: The easiest way to split a string into newline in .NET?

An alternative way is to use StringReader:

 using (System.IO.StringReader reader = new System.IO.StringReader(input)) { string line = reader.ReadLine(); } 
+1
source share

Your answer is: theStringYouGot.Split('\n'); where you get an array of strings to process.

-one
source share

All Articles