本文介绍了比较字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我在比较2个字符串时遇到困难,我通过串口连接收到R并希望比较在if语句中它似乎不起作用。

I'm having difficulty with comparing 2 strings, i receive R over serial connection and want to compare that in an if statement but it doesn't seem to work.

我有以下代码:

private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {

	dataIN = serialPort1->ReadExisting();
	this->Invoke(gcnew System::EventHandler(this, &MyForm::ShowData));

}

private: void ShowData(System::Object^ sender, System::EventArgs^ e)
{
	tBoxDataIn->Text += dataIN;

	if (dataIN == "R") {
		//debug
	MessageBox::Show("ayyy lmao");
	}


}

谢谢

推荐答案

(在C ++中为样本选择右边的语言C ++)

(choose Language C++ on the right for the samples in C++)

谢谢回复。

我之前尝试过这段代码:

I've tried this code before:

private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {

	dataIN = serialPort1->ReadExisting();
	this->Invoke(gcnew System::EventHandler(this, &MyForm::ShowData));

}

private: void ShowData(System::Object^ sender, System::EventArgs^ e)
{
	tBoxDataIn->Text += dataIN;

	if (dataIN->StartsWith("R")) {
		//debug
		MessageBox::Show("Ready");
	}
}

但是,我可以看到dataIN等于"R"。因此必须以"R"开头。然而它不会触发消息框

However, i can see dataIN is equal to "R" and thus must start with "R" however it doesn't trigger the messagebox

PS:我知道它必须是R,因为它写的是"R"。 to tBoxDataIN(winforms中的文本框)

PS: i know it must be R because it writes "R" to tBoxDataIN (textbox in winforms)





这篇关于比较字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:32