2015-10-18 33 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

谢谢你的回答,我敢打赌这是正确的,我得到一些错误。不过谢谢你。 –

相关问题