//待分割字符 String split_input= "My_i_You_i_Her_i_XiaoHe_i_"; //分割结果 String split_result[10];//手动动态调整数组大小,保证数组可以满足容量 void setup() { Serial.begin(115200); //调用分割 Split(split_input,"_i_",split_result);//分割调用 //打印结果 for(int i=0;i<10;i++) { if(split_result[i]!="") { Serial.println(String(i)+split_result[i]); } } } void loop() { } void Split(String zifuchuan,String fengefu,String result[]) { int weizhi; //找查的位置 String temps;//临时字符串 int i=0; do { weizhi = zifuchuan.indexOf(fengefu);//找到位置 if(weizhi != -1)//如果位置不为空 { temps=zifuchuan.substring(0,weizhi);//打印取第一个字符 zifuchuan = zifuchuan.substring(weizhi+fengefu.length(), zifuchuan.length()); //分隔后只取后面一段内容 以方便后面找查 } else { //上面实在找不到了就把最后的 一个分割值赋值出来以免遗漏 if(zifuchuan.length() > 0) temps=zifuchuan; } result[i++]=temps; //Serial.println(result[i-1]);//在这里执行分割出来的字符下面不然又清空了 temps=""; } while(weizhi >=0); }