2015-10-18 35 views
1

想知道如何分割每個字符串並獲取單詞的數量。但是我不斷收到一個錯誤'Split':不是'System :: Array'的成員,在第三行中有分割或片斷。如何通過C++中的單詞知道每個字符串的長度

String^ originalString = textBox1->Text;//original text string 
cli::array<String^>^ piece= originalString->Split('.');//text is being split into sentences  
cli::array<String^>^ sentence = piece->Split(' ');// text is being split into words, also I get error here 
for (int i = 0; i < sentence->Length; ++i) { 
datagridview1->Rows[i]->Cells[2]->Value = i;} 

回答

1

我覺得你可以做最簡單的事情是使用Regex

String^ text = "This is a chord. This is another. This is a third. Now form a band."; 

int wordCount = Regex::Matches(text, "\\w+")->Count; // = 15 

其中

\w代表「單詞字符」。它總是匹配ASCII字符[A-Za-z0-9_]。注意包含下劃線和數字。

Shorthand Character Classes


更新到:

,但我需要在每個句子

在此情況下,這應該爲你工作中的一些詞:

using namespace System; 
using namespace System::Collections::Generic; 
using namespace System::Diagnostics; 
using namespace System::Linq; 
using namespace System::Text::RegularExpressions; 

static int CountWords(String^ text) 
{ 
    return Regex::Matches(text, "\\w+")->Count; 
} 

int main(array<System::String ^> ^args) 
{ 
    String^ text = "This is a chord. This is another. This is a third. Now form a band."; 

    // split sentences 
    IEnumerable<String^>^ sentences = Regex::Split(text, "[.!?](?!$)"); 
    List<int>^ wordCounts = Enumerable::ToList(
     // count words for each sentence 
     Enumerable::Select<String^, int>(sentences, gcnew Func<String^, int>(&CountWords))); 
} 

其中:

  • [.!?]匹配任何這三個句子結尾的,因此拆分文本有
  • (?!$)這是一個負先行?!,它確保結束.!?最後一句是不是結束文本$這將導致一個空字符串
+0

這是一個很好的例子,但是我在每個句子中都需要一些單詞。所以根據你的String ^文本,我應該得到答案4; 3; 4; 4. –

+0

這個信息不包括在你的問題中,不是很清楚,所以我不知道它; - ] – t3chb0t

+0

@DeividasKiznis我更新了我的答案。你可以檢查新的解決方案是否滿足你的要求;-) – t3chb0t

2

您可以通過獲取句子,這是由分隔的單詞組開始了「」字符,然後爲每個句子獲取單詞,這些單詞由空白字符分隔。

using namespace System; 
using namespace System::Collections::Generic; 
using namespace System::Diagnostics; 

String^ originalString = "This is a chord. This is another. This is a third. Now form a band."; 

// This array contains the sentences, which are separated by '.' 
array<String^>^ sentences = originalString->Split(
    gcnew array<String^> { "." }, 
    StringSplitOptions::RemoveEmptyEntries); 

Debug::Assert(sentences->Length == 4); 

// This list contains individual words for all sentences. 
List<String^>^ words = gcnew List<String^>(); 
for each(String^ sentence in sentences) { 
    words->AddRange(sentence->Split(
     gcnew array<String^> { " " }, 
     StringSplitOptions::RemoveEmptyEntries)); 
} 

Debug::Assert(words->Count == 15); 

for each(String^ word in words) { 
    Console::WriteLine(word); 
} 

但是,如果你有興趣的唯一的事情是個人,您可以使用讓他們在一個單一的表達LINQ:

using namespace System; 
using namespace System::Collections::Generic; 
using namespace System::Diagnostics; 
using namespace System::Linq; 

System::String^ StripDot(System::String^ input) { 
    return input->Replace(".", ""); 
} 

void Test() 
{ 
    String^ originalString = "This is a chord. This is another. This is a third. Now form a band."; 

    IEnumerable<String^>^ words = Enumerable::Select<String^,String^>(
     originalString->Split(
      gcnew array<String^> { " " }, 
      StringSplitOptions::RemoveEmptyEntries), 
     gcnew Func<String^,String^>(StripDot)); 

    Debug::Assert(Enumerable::Count(words) == 15); 

    for each(String^ word in words) { 
     Console::WriteLine(word); 
    } 
} 
+0

謝謝你的回答,我敢打賭這是正確的,我得到一些錯誤。不過謝謝你。 –

相關問題