7

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?
    }
}
puretppc
  • 3,232
  • 8
  • 38
  • 65
darthwillard
  • 809
  • 2
  • 14
  • 28

4 Answers4

24

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);
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • i understand how to use this, but i want to treat each of the result strings as substrings so i can analyze them – darthwillard Feb 02 '14 at 18:43
  • The substrings are in the array result each one at its own index. Sorry but it is not clear, could you give an example? – Steve Feb 02 '14 at 18:44
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]

L.B
  • 114,136
  • 19
  • 178
  • 224
  • so now, i need to treat those values returned as strings, how do i evaluate them every time? i don't want to just print them, how do i retrieve them to analyze each one separately> – darthwillard Feb 02 '14 at 18:42
  • @darthwillard `last3Digits` is a `List`. I don't understand what you mean by *`how do i evaluate them every time`* Do you want to iterate over it. then use for loop. `foreach(var i in last3Digits)` – L.B Feb 02 '14 at 18:43
1

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();
}
Community
  • 1
  • 1
Louis
  • 81
  • 1
  • 3
-1

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

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78