我在这里写了代码,虽然有效但需要拼命改进。
//////////////////////Split into sentence/////////////////////////
String^ text = textBox1->Text;
cli::array<String^>^ sentence = text->Split('.', '?', '!');
for (int i = 0; i < sentence->Length; ++i) {
datagridview->Rows->Add();
datagridview->Rows[i]->Cells[1]->Value = i + 1;
datagridview->Rows[i]->Cells[3]->Value = sentence[i];
}
//////////////////////Split into words/////////////////////////
cli::array<String^>^ word = text->Split(' ');
for (int ii = 0; ii < word->Length; ++ii) {
datagridview->Rows[ii]->Cells[4]->Value = ii + 1;
datagridview->Rows[ii]->Cells[5]->Value = word[ii];
datagridview->Rows->Add();
}
在代码文本中输入并分割句子和单词。在下面的图片中,您将看到我的代码输出:
如你所见,句子长度不起作用。
答案 0 :(得分:2)
您实际上并没有在代码中填充句子长度单元格(第2列),这就是为什么没有出现在那里的原因。
你需要这些内容:
String^ text = textBox1->Text;
cli::array<String^>^ sentences = text->Split('.', '?', '!');
for each (String^ sentence in sentences) {
cli::array<String^>^ words = sentence->Split(' ');
for each (String^ word in words) {
int rowIndex = datagridview->Rows->Add();
datagridview->Rows[rowIndex]->Cells[1]->Value = i + 1;
datagridview->Rows[rowIndex]->Cells[2]->Value = sentence->Length; // This is the line you're missing
datagridview->Rows[rowIndex]->Cells[3]->Value = sentence;
datagridview->Rows[rowIndex]->Cells[4]->Value = ii + 1;
datagridview->Rows[rowIndex]->Cells[5]->Value = word;
}
}
编辑 - 然后尝试:
String^ text = textBox1->Text;
cli::array<String^>^ sentences = text->Split('.', '?', '!');
for (int sentenceIndex = 0; sentenceIndex < sentences->Length; ++sentenceIndex) {
String^ sentence = sentences[sentenceIndex];
cli::array<String^>^ words = sentence->Split(' ');
for (int wordIndex = 0; wordIndex < words->Length; ++wordIndex) {
int rowIndex = datagridview->Rows->Add();
datagridview->Rows[rowIndex]->Cells[1]->Value = sentenceIndex + 1;
datagridview->Rows[rowIndex]->Cells[2]->Value = sentence->Length; // This is the line you're missing
datagridview->Rows[rowIndex]->Cells[3]->Value = sentence;
datagridview->Rows[rowIndex]->Cells[4]->Value = wordIndex + 1;
datagridview->Rows[rowIndex]->Cells[5]->Value = words[wordIndex];;
}
}