在 Arduino 中查找字符串是否以一组特定字符开头
Arduino 中的 startsWith() 函数可帮助您确定字符串是否以一组特定字符开头。如果字符串以您指定的子字符串开头,则它将返回 true,否则将返回 false。下面给出示例实现 -
示例
void setup() { // put your setup code here, to run once: Serial.begin(9600); String String1 = "AB_Test"; String String2 = "CD_Test"; String substr1 = "AB"; String substr2 = "AB*"; if(String1.startsWith(substr1)){ Serial.print("String1 starts with substr1"); } if(String2.startsWith(substr1)){ Serial.print("String2 starts with substr1"); } if(String1.startsWith(substr2)){ Serial.print("String1 starts with substr2"); } if(String2.startsWith(substr2)){ Serial.print("String2 starts with substr2"); } } void loop() { // put your main code here, to run repeatedly }
正如您所料,此程序将仅打印“String1 starts with substr1”。下面显示了相应的串行监视器输出(开头处的垃圾字符是编程/启动噪声) -
输出
广告