如何在C++中通过单词知道每个字符串的长度
How to know each string length by words in C++
想知道如何拆分每个字符串并得到单词数。但是我一直收到错误 'Split': is not a member of 'System::Array' with a split or piece in the third line.
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;}
您可以先获取 句子 ,它们是由“.”分隔的词组。字符,然后得到每个句子的个单词,由一个空格字符分隔。
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);
}
}
我认为你能做的最简单的事情就是使用 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
stands for "word character". It always matches the ASCII characters [A-Za-z0-9_]
. Notice the inclusion of the underscore and digits.
更新为:
but I need a number of words in each sentence
在这种情况下,这应该适合你:
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)));
}
其中:
[.!?]
匹配这三个句子结尾中的任何一个,因此将文本拆分到那里
(?!$)
它是一个否定前瞻 ?!
,它确保以 .!?
结尾的最后一句话不在文本 $
的末尾,这将导致空字符串
想知道如何拆分每个字符串并得到单词数。但是我一直收到错误 'Split': is not a member of 'System::Array' with a split or piece in the third line.
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;}
您可以先获取 句子 ,它们是由“.”分隔的词组。字符,然后得到每个句子的个单词,由一个空格字符分隔。
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);
}
}
我认为你能做的最简单的事情就是使用 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
stands for "word character". It always matches the ASCII characters[A-Za-z0-9_]
. Notice the inclusion of the underscore and digits.
更新为:
but I need a number of words in each sentence
在这种情况下,这应该适合你:
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)));
}
其中:
[.!?]
匹配这三个句子结尾中的任何一个,因此将文本拆分到那里(?!$)
它是一个否定前瞻?!
,它确保以.!?
结尾的最后一句话不在文本$
的末尾,这将导致空字符串