Ben Wilson
Ben Wilson

Reputation: 3137

split a string from a text file into another list

Hi i know the Title might sound a little confusing but im reading in a text file with many lines of data

Example
12345 Test 
34567 Test2

i read in the text 1 line at a time and add to a list

using (StreamReader reader = new StreamReader("Test.txt"))
                    {

                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            list.Add(line);

                    }

                }

how do i then separate the 1234 from the test so i can pull only the first column of data if i need like list(1).pars[1] would be 12345 and list(2).pars[2] would be test2

i know this sounds foggy but i hope someone out there understands

Upvotes: 0

Views: 4314

Answers (5)

Adriano Repetti
Adriano Repetti

Reputation: 67128

When you have a string of text you can use the Split() method to split it in many parts. If you're sure every word (separated by one or more spaces) is a column you can simply write:

string[] columns = line.Split(' ');

There are several overloads of that function, you can specify if blank fields are skipped (you may have, for example columns[1] empty in a line composed by 2 words but separated by two spaces). If you're sure about the number of columns you can fix that limit too (so if any text after the last column will be treated as a single field).

In your case (add to the list only the first column) you may write:

if (String.IsNullOrWhiteSpace(line))
 continue;

string[] columns = line.TrimLeft().Split(new char[] { ' ' }, 2);
list.Add(columns[0]);

First check is to skip empty or lines composed just of spaces. The TrimLeft() is to remove spaces from beginning of the line (if any). The first column can't be empty (because the TrimLeft() so yo do not even need to use StringSplitOptions.RemoveEmptyEntries with an additional if (columns.Length > 1). Finally, if the file is small enough you can read it in memory with a single call to File.ReadAllLines() and simplify everything with a little of LINQ:

list.Add(
 File.ReadAllLines("test.txt")
 .Where(x => !String.IsNullOrWhiteSpace(x))
 .Select(x => x.TrimLeft().Split(new char[] { ' ' }, 2)[0]));

Note that with the first parameter you can specify more than one valid separator.

Upvotes: 1

Arion
Arion

Reputation: 31249

Maybe something like this:

string test="12345 Test";
var ls= test.Split(' ');

This will get you a array of string. You can get them with ls[0] and ls[1].

If you just what the 12345 then ls[0] is the one to choose.

Upvotes: 2

Serj-Tm
Serj-Tm

Reputation: 16981

  var splitted = System.IO.File.ReadAllLines("Test.txt")
      .Select(line => line.Split(' ')).ToArray();
  var list1 = splitted.Select(split_line => split_line[0]).ToArray();
  var list2 = splitted.Select(split_line => split_line[1]).ToArray();

Upvotes: 0

Andrew
Andrew

Reputation: 7788

When you have multiple spaces

Regex r = new Regex(" +");
string [] splitString = r.Split(stringWithMultipleSpaces);

Upvotes: 0

Khan
Khan

Reputation: 18162

If you're ok with having a list of string[]'s you can simply do this:

        var list = new List<string[]>();
        using (StreamReader reader = new StreamReader("Test.txt"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                list.Add(line.Split(' '));
            }
        }

        string firstWord = list[0][0]; //12345 
        string secondWord = list[0][1]; //Test

Upvotes: 1

Related Questions