Arduino String.indexOf()不工作
我已经做了好几个小时了。即使在串行监视器中打印字符时,indexOf()函数也不会返回>-1,并且字符串的长度随着字符的输入而增加Arduino String.indexOf()不工作,string,bluetooth,arduino,char,indexof,String,Bluetooth,Arduino,Char,Indexof,我已经做了好几个小时了。即使在串行监视器中打印字符时,indexOf()函数也不会返回>-1,并且字符串的长度随着字符的输入而增加 String command; void loop() { while ( bleuart.available() ) { char ch; ch = bleuart.read(); command += ch; Serial.print(ch
String command;
void loop() {
while ( bleuart.available() ) {
char ch;
ch = bleuart.read();
command += ch;
Serial.print(ch);
}
Serial.println(command.length());
if(command.indexOf("\n") > -1 ) {
Serial.println("command:");
Serial.println(command);
}
}
@darc是正确的,但如果字符串来自ble模块,则字符串中不应该有实际的\n,我假设是这样。这应该是一个新行字符(DEC 10),它应该与相同 我在代码中看到3个不同的问题
命令
变量永远不会被清除,因此它只会增长到
内存不足。完成该命令后,调用
command=”“代码>
command.concat(ch)代码>而不是命令+=ch代码>
if(command.indexOf(“\n”)>-1)
更改为if(command.indexOf(“\n”)!=-1)
看看这是否有帮助。您是否在串行监视器中看到新行?或者只是一根长绳子\n是新行字符,只是一个长字符串-没有新行\n似乎什么也不做。如果在字符串中看到\n,则这实际上是“\\”+“n”字符。仅用于检查change indexof以查找“\\n”,并查看是否match@darc这成功了!我不太明白为什么。“\n”如何变成“\\n”?基本上,\是一个特殊字符,用于转义序列,例如“\n”代表新行。因此,如果要将\打印为字符串,则需要添加\以将其转义为“\\”,另一个示例是:假设您想在字符串中使用“您需要将其转义为\像这样的“\”。您可以在此处看到转义序列的完整列表“