Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split strings into many strings by newline?

i have incoming data that needs to be split into multiple values...ie.

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

The length is inconsistent when i receive it, the spacing is inconsistent when it is present, and i want to analyze the last 3 digits before each \n. how do i break off the string and turn it into a new string? like i said, this round, it may have 3 \n commands, next time, it may have 10, how do i create 3 new strings, analyze them, then destroy them before the next 10 come in?

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?
    }
}
like image 1000
darthwillard Avatar asked Feb 02 '14 18:02

darthwillard


1 Answers

You could use the string.Split method. In particular I suggest to use the overload that use a string array of possible separators. This because splitting on the newline character poses an unique problem. In you example all the newline chars are simply a '\n', but for some OS the newline char is '\r\n' and if you can't rule out the possibility to have the twos in the same file then

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

Instead if your are certain that the file contains only the newline separator allowed by your OS then you could use

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

The StringSplitOptions.RemoveEmptyEntries allows to capture a pair of consecutive newline or an ending newline as an empty string.

Now you can work on the array examining the last 3 digits of every string

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 index of the newline character without splitting the original string, you could use string.IndexOf in 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);
    }
}
like image 87
Steve Avatar answered Oct 02 '22 04:10

Steve