retrieve values by delimiters

60 views Asked by At

I have stored a text file into isolated storage, my file is saved in a way of "asdasdasd,asdasdasd", how can I use delimiters to spilt them instead of retrieving the whole string like this:

        //Get the Store we created earlier
        IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
        //use a StreamReader to open and read the file           
        StreamReader readFile = null;
        try
        {
            readFile = new StreamReader(new IsolatedStorageFileStream("SaveFolder\\SavedFile.txt", FileMode.Open, store));
            string fileText = readFile.ReadToEnd();

            //The control txtRead will display the text entered in the file
            textBlock2.Text = fileText;
            readFile.Close();
        }

        catch
        {
            //For now, a simple catch to make sure they created it first
            //we will modify this later
            textBlock2.Text = "Need to create directory and the file first.";
        }
2

There are 2 answers

5
Konrad Kokosa On BEST ANSWER

You can use string.Split method to split string delimited by the specified characters:

string fileText = readFile.ReadToEnd();
string[] substrings = fileText.Split(',');

Then you can access each substring by an array index, for example:

textBlock1.Text = substrings[0];
textBlock2.Text = substrings[1];

Note: It would be better to check if substrings array have at least those two elements.

1
Ahmed ilyas On

to split them, split the fileText like so:

string fileText = readFile.ReadToEnd();

string[] splitText = fileText.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);

you now have an array in the splitText. It now contains individual entries