Search code examples
c#filestream

Splitting data from text file in C#


How can I split data from a text file, for example, I have this text file

"tarek 5235263463 US"

in C++ it would be like

    string name , number , location;
    file >> name >> number >> location;

How can I do the same in C#?


Solution

  • Let's assume you already have the file content as a string (it shouldn't be too difficult to achieve that, should it?). Then you'll first have to split the string:

    var splitted = inputString.Split();
    

    The splitted string now contains the information you want, so all you need to do now is:

    var name = splitted[0];
    var number = splitted[1];
    var location = splitted[2];
    

    That should do what you wanted to do.