0

For example,

string example = "Useless info\nI want this line in it's own string or part of a string[]\nUseless info"

How do I get only the second line?

Pop Car
  • 363
  • 5
  • 15

4 Answers4

2

Use string splitting functions:

string[] split = example.Split('\n');

Each of the array items will be a line. Then you can access the one you want by the index.

rualmar
  • 229
  • 2
  • 8
2
var secondLine = example.Split('\n')[1];
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
0

You can use this code:

string[] text=example.split("\n");

text[1] will be the second line content.

0

Try this. Split to newline by using \r\n or \n

    string example = "Useless info\nI want this line in it's own string or part of a string[]\nUseless info";
    string[] lines = example.Split(new string[]
    {
    "\r\n", "\n"
    }

    , StringSplitOptions.None);

    foreach (var line in lines)
    {
        Console.WriteLine(line);
    }
active92
  • 644
  • 12
  • 24