2015-01-04 82 views
-2

我讀文本文件轉換成一個字符串,然後將整個文件劈裂成字符串數組與此代碼:拆分大文本字符串分成若干小的

string[] split_text = Regex.Split(whole_text, @"\W+"); 

但是,當我這樣做,每一句話都是獨自在一個索引,我不想要那個。

我想要在一個索引biger字符串讓說約10個字在數組中的一個索引,然後10個字在第二個索引等等。

所以,如果我讀90個字的文件,我想有數組9的大小和每個索引10個單詞。

+0

是所有'90'字出現在一行? – 2015-01-04 12:15:32

+1

你試過'「\ W {10}」'? – 2015-01-04 12:17:52

+0

你可以嘗試匹配而不是拆分。 http://ideone.com/bbzhFV – 2015-01-04 12:25:47

回答

2

您可以使用Batch方法:

string[] split_text = Regex.Split(whole_text, @"\W+") 
       .Batch(10) 
       .Select(x => string.Concat(x)) 
       .ToArray(); 
+0

'System.Array'不包含'Batch'的定義。我得到這個錯誤,我必須包括一些東西嗎? @ Selman22 – user3127680 2015-01-04 12:23:42

+0

是的,你需要下載MoreLINQ並添加一個引用它。 – 2015-01-04 12:24:07

+0

我用這個鏈接安裝MoreLINQ:[link](https://www.nuget.org/packages/MoreLinq.Source.MoreEnumerable/),然後我使用這個代碼包含了Linq,使用System.Linq,我仍然可以' t使用'.Batch' @ Selman22 – user3127680 2015-01-04 13:16:34

1

確定有充分sollution:

class Program 
{ 
    static void Main(string[] args) 
    { 

     List<string> result = new List<string>(); 
     string text = "Now im checking first ten chars from sentence and some random chars : asdasdasdasdasdasdasdasd"; 
     try 
     { 
      for (int i = 0; i < text.Length; i = i + 10) 
      { 
       string res = text.Substring(i,10); 
       result.Add(res); 
       Console.WriteLine(res); 
      } 
     } 
     catch (Exception) 
     { 
     } 
    } 
} 

我建議使用List<string>而不是字符串數組。