arduino - 如何在arduino中将字符串拆分为单词?
问题描述
我在arduino中有一个字符串
String name="apple orange banana";
是否可以将每个项目存储在数组中 arr
以便
arr[0]="apple"
arr[1]="orange" ......etc
如果不将它们存储在单个变量中?
解决方案
如何使用 Arduino 中的特定分隔符拆分字符串?我相信这会对你有所帮助,你可以做一个 while 循环,如:
int x;
String words[3];
while(getValue(name, ' ', x) != NULL){
words[x] = getValue(name, ' ', x);
}
使用此功能:
// https://stackoverflow.com/questions/9072320/split-string-into-string-array
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
推荐阅读
- arrays - 查看数组是否按降序排列时跳过数组中的特定数字
- reactjs - react bootstrap 上的列显示,尽管被声明为 2 列,但仅显示为 1 列
- java - spring 在哪里查找带有 @Value 注释的值
- python - 根据键拆分字典
- python - 如果它们包含子字符串,我将附加到新列表,如果不包含,则附加到新列表。第二个列表得到了第一个的条件,为什么?
- intellij-idea - 将 IntelliJ 的控制台输出通过管道传输到脚本
- docker - 运行没有入口点的命令
- python - 创建一个列,它是熊猫中两个字符串列的差异
- python - 如果输入不足,如何遍历文件并引发自定义异常?
- python - 如何对字符串列表中的元素进行编号并返回一个新列表?