Search code examples
c#stringsplitusbstreamreader

Splitting one line of string into different variables, from a TextFile


I'm trying to parse a string from a text file, and somehow split the elements, and use them in separate variables. The string takes similar form to the following:

TEST DISK,3819.9609375,3819.96875,FAT32

Now I'm using StreamReader to get the information from the text file, and my first thought was to use String.Split (Hence the Commas), but I couldn't find a way to get each segment into a different variable, like:

  • Variable 1: TEST DISK
  • Variable 2: 3819.9609375
  • Variable 3: 3819.96875
  • Variable 4: FAT32

My question is how can I get this string into a similar format above, if so, is there a way it can be done using String.Split()? Cheers


Solution

  • This code works for me:

    string s = "TEST DISK,3819.9609375,3819.96875,FAT32";
    string[] vars = s.Split(',');
    

    Output:

    vars[0] = "TEST DISK"
    vars[1] = "3819.9609375"
    vars[2] = "3819.96875"
    vars[3] = "FAT32"