2013-12-09 46 views
0

我已經將文本文件存儲到獨立存儲中,我的文件以「asdasdasd,asdasdasd」的方式保存,我如何使用分隔符來溢出它們而不是像這樣檢索整個字符串:通過分隔符檢索值

 //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."; 
     } 

回答

1

您可以使用string.Split方法拆分由指定的字符分隔的字符串:

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

然後你就可以通過array index訪問每個字符串,例如:

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

注意:最好是檢查substrings數組是否至少有這兩個元素。

+0

我該如何使用strs數組? – NoobieNeedHelp

+0

@NoobieNeedHelp,這取決於你想用它做什麼 –

+0

示例放入一個文本框textBlock2.Text = fileText; readFile.Close(); – NoobieNeedHelp

0

分裂他們,分裂FILETEXT像這樣:

string fileText = readFile.ReadToEnd(); 

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

你現在在splitText的數組。它現在包含個人條目

+0

我怎樣才能使用spiltText的數組? – NoobieNeedHelp