2

I have he following issue:

String line = @"Line1
            Line2
            Line3
            Line4";

I am trying to create a loop that detects when the carriage return character is encountered and store each row on a separate string.

String value    
static long LinesCount(string s)
        {
        long count = 0;
        int position = 0;
        while ((position = s.IndexOf('\n', position)) != -1)
            {
            count++;           
            }
        return count;
        }

for (int i = 1; i > LinesCount(line); i++)
        {
            value = line.Split(Environment.NewLine)
        }
leppie
  • 115,091
  • 17
  • 196
  • 297
user3191666
  • 159
  • 1
  • 5
  • 20

1 Answers1

1

trying to create a loop that detects when the carriage return character is encountered and store each row on a separate string.

just split line by '\n' character. resulting array contains expected rows

String line = @"Line1
    Line2
    Line3
    Line4";
string[] lines = line.Split('\n');
foreach(var L in lines)
{
    Console.Write(L);
    Console.WriteLine(';'); // for demonstration purpose
}

console output

Line1;
            Line2;
            Line3;
            Line4;

demo

ASh
  • 34,632
  • 9
  • 60
  • 82