Van Man
Van Man

Reputation: 51

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:

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

Upvotes: 0

Views: 2167

Answers (2)

Kurubaran
Kurubaran

Reputation: 8902

try this,

string[] line = File.ReadAllLines("FilePath");

if (null != line && line.Length > 0)
{
    string[] values = line[0].Split(new char[',']);
    string variable1 = values[0]; //TEST DISK
    string variable2 = values[1]; //3819.9609375
}

Upvotes: 0

etaiso
etaiso

Reputation: 2746

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"

Upvotes: 1

Related Questions