split strings into many strings by newline?

倖福魔咒の 提交于 2019-12-04 21:40:29

问题


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?
    }
}

回答1:


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);
    }
}



回答2:


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]




回答3:


This has been answered before, check this thread: Easiest way to split a string on newlines in .NET?

An alternative way is using StringReader:

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



回答4:


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



来源:https://stackoverflow.com/questions/21514387/split-strings-into-many-strings-by-newline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!