2016-06-17 69 views
1

我想讀取包含這個字符串變量:我怎麼可以檢索保持他們爲了多子串

== == TITLE1

文本1 ...

== ==標題2

text2 ...

。 。 。 等

我想要檢索TITLE1,標題2和等子字符串,然後創建單獨的文件是這樣的:

title1.txt包含文本1

title2.txt包含文本2

哪能做這個?

回答

0
string[] split = yourString.Split(new string[] { "==" }, StringSplitOptions.None); 

這會給你這些entrys的數組:TITLE1,文本1,標題2,文本2等 現在,當你通過數組進行迭代,你將有標題每秒循環。所以增加2,你將在我和i + 1相應的文本的標題。

for (int i = 0; i < split.Length; i+=2){ 
    File.WriteAllText("yourPath/" + split[i] + ".txt", split[i+1]); 
} 
0

我通過改變代碼解決它這樣:

_edit = edit.InnerText.Replace("\n", Environment.NewLine); 
        // _edit = edit.InnerText.Replace("<lt;ref&", "<ref>"); 
        // _edit = edit.InnerText.Replace("&lt;/ref>", "</ref>"); 
        string[] split = _edit.Split(new[] { "==" }, StringSplitOptions.None); 

        System.IO.Directory.CreateDirectory(path + title); 
        using (StreamWriter sw = new StreamWriter(path + title + "\\" + "مقدمه.txt")) 
        { 
         sw.WriteLine(split[0]); 
        } 
        for (int i = 1; i < split.Length; i += 2) 
        { 

         //split[i].Replace("=", " "); 
         //File.WriteAllText(path + split[i] + ".txt", split[i + 1]); 
         using (StreamWriter sw = new StreamWriter(path + title + "\\" + split[i] + ".txt")) 
         { 
          // Add some text to the file. 
          sw.WriteLine(split[i + 1]); 
         } 
        }