C++ cli 如何通过C++;

C++ cli 如何通过C++;,c++-cli,C++ Cli,想知道如何分割每个字符串并获取单词数。但我一直收到一个错误“Split”:不是第三行中有Split或part的“System::Array”成员 String^ originalString = textBox1->Text;//original text string cli::array<String^>^ piece= originalString->Split('.');//text is being split into sentences cli::a

想知道如何分割每个字符串并获取单词数。但我一直收到一个错误“Split”:不是第三行中有Split或part的“System::Array”成员

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;}
String^originalString=textBox1->Text//原始文本字符串
cli::数组^piece=originalString->Split('.')//文本被分成了几个句子
cli::数组^SENTURE=piece->Split(“”);//文本被拆分成单词,这里也有错误
对于(int i=0;i<句子->长度;++i){
datagridview1->Rows[i]->Cells[2]->Value=i;}
您可以从获取句子开始,这些句子由一个“.”字符分隔,然后获取每个句子的单词,这些单词由一个空白字符分隔

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);
}
使用名称空间系统;
使用命名空间System::Collections::Generic;
使用名称空间系统::诊断;
String^originalString=“这是一个和弦。这是另一个和弦。这是第三个和弦。现在组成一个乐队。”;
//此数组包含以“.”分隔的句子
数组^句子=原始字符串->拆分(
gcnew数组{“.”,
StringSplitOptions::RemoveEmptyEntries);
调试::断言(句子->长度==4);
//此列表包含所有句子的单个单词。
List^words=gcnewlist();
对于每个(句子中的字符串和句子){
单词->添加范围(句子->拆分)(
gcnew数组{“”},
StringSplitOptions::RemoveEmptyEntries));
}
调试::断言(单词->计数==15);
对于每个(单词中的字符串^word){
控制台::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);
    }
}
使用名称空间系统;
使用命名空间System::Collections::Generic;
使用名称空间系统::诊断;
使用名称空间系统::Linq;
系统::字符串^StripDot(系统::字符串^input){
返回输入->替换(“.”,“”);
}
无效测试()
{
String^originalString=“这是一个和弦。这是另一个和弦。这是第三个和弦。现在组成一个乐队。”;
IEnumerable^words=可枚举::选择(
原始字符串->拆分(
gcnew数组{“”},
StringSplitOptions::RemoveEmptyEntries),
gcnew Func(条纹点);
调试::断言(可枚举::计数(字)==15);
对于每个(单词中的字符串^word){
控制台::WriteLine(word);
}
}

我认为你能做的最简单的事情就是使用
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.]
。请注意包含下划线和数字


更新至:

但我需要在每句话中加入一些单词

在这种情况下,这应该适用于您:

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)));
}
使用名称空间系统;
使用命名空间System::Collections::Generic;
使用名称空间系统::诊断;
使用名称空间系统::Linq;
使用命名空间System::Text::RegularExpressions;
静态整数CountWords(字符串^text)
{
返回Regex::Matches(文本“\\w+”)->Count;
}
int main(数组^args)
{
String^text=“这是一个和弦。这是另一个。这是第三个。现在组成一个乐队。”;
//分句
IEnumerable^句子=Regex::Split(文本,“[.!?]”(?!$)”;
列表^wordCounts=可枚举::ToList(
//每句话数一数单词
可枚举::选择(句子、新函数(&CountWords));
}
其中:

  • [.!?]
    匹配这三个句子结尾中的任何一个,从而将文本拆分到那里
  • (?!$)
    这是一个消极的前瞻
    ,它确保最后一句以
    结尾
    不在文本
    $
    的末尾,这将导致一个空字符串

谢谢你的回答,我打赌它是正确的,但你能给出更简单的变体吗,因为当我尝试使用时,我很少出错。尽管如此,还是要谢谢你。这是一个很好的例子,但我需要在每个句子中加入一些单词。所以根据你的字符串,我应该得到答案4;3.4.4.这个信息没有包含在你的问题中,顺便说一下,这个问题不是很清楚,所以我不可能知道;-]@我更新了我的答案。您可以检查新解决方案是否满足您的要求;-)代码看起来很好,但当复制代码时,我会遇到一大堆错误:比如我的命名空间不能识别Linq和其他一些单词,比如IEnumerable或Regex(即使我包含了库)。我不知道这有多重要,但我的文件扩展名是h而不是cpp。顺便说一句,我真的很感谢你的帮助。很难说为什么它在你的项目中不起作用。如果您喜欢,我已经将我的测试项目上传到github,您可以在这里找到它:它是用VS20015社区创建的;-)c++/cli有时很奇怪,我在linq上也遇到了一个小问题,我添加了对System.Core的引用,它可能修复了这个问题。