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) {
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); } }
Steve
source share