将String拆分为String数组

我一直在为arduino编程,但今天我遇到了一个问题,我无法用我非常有限的C知识来解决。 这是怎么回事。 我正在创建一个将串行输入发送到arduino(deviceID,command,commandparameters)的pc应用程序。 这个arduino将把这个命令通过RF发送给其他arduino。 根据deviceID,正确的arduino将执行命令。

为了能够确定我想在“,”上拆分该字符串的deviceID。 这是我的问题,我知道如何在java中轻松地做到这一点(即使不使用标准的分割function),但在C中它是一个完全不同的故事。

你们中的任何人都能告诉我如何让这个工作吗?

谢谢

/* Serial Event example When new serial data arrives, this sketch adds it to a String. When a newline is received, the loop prints the string and clears it. A good test for this is to try it with a GPS receiver that sends out NMEA 0183 sentences. Created 9 May 2011 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/SerialEvent */ String inputString; // a string to hold incoming data boolean stringComplete = false; // whether the string is complete String[] receivedData; void setup() { // initialize serial: Serial.begin(9600); // reserve 200 bytes for the inputString: inputString.reserve(200); } void loop() { // print the string when a newline arrives: if (stringComplete) { Serial.println(inputString); // clear the string: inputString = ""; stringComplete = false; } } /* SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can delay response. Multiple bytes of data may be available. */ void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); if (inChar == '\n') { stringComplete = true; } // add it to the inputString: if(stringComplete == false) { inputString += inChar; } // if the incoming character is a newline, set a flag // so the main loop can do something about it: } } String[] splitCommand(String text, char splitChar) { int splitCount = countSplitCharacters(text, splitChar); String returnValue[splitCount]; int index = -1; int index2; for(int i = 0; i < splitCount - 1; i++) { index = text.indexOf(splitChar, index + 1); index2 = text.indexOf(splitChar, index + 1); if(index2  -1) { index = text.indexOf(splitChar, index + 1); if(index > -1) returnValue+=1; } return returnValue; } 

我已经决定要使用strtok函数了。 我现在遇到了另一个问题。 发生的错误是

SerialEvent.cpp:在函数’void splitCommand(String,char)’中:

SerialEvent:68:错误:无法将参数’1’的’String’转换为’char *’为’char * strtok(char *,const char *)’

SerialEvent:68:错误:在此范围内未声明’null’

代码就像,

 String inputString; // a string to hold incoming data void splitCommand(String text, char splitChar) { String temp; int index = -1; int index2; for(temp = strtok(text, splitChar); temp; temp = strtok(null, splitChar)) { Serial.println(temp); } for(int i = 0; i < 3; i++) { Serial.println(command[i]); } } 

这是一个老问题,但我创建了一些可能有用的代码:

  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]) : ""; } 

此函数返回由给定索引处的预定义字符分隔的单个字符串。 例如:

 String split = "hi this is a split test"; String word3 = getValue(split, ' ', 2); Serial.println(word3); 

应打印’是’。 您也可以尝试索引0返回’hi’或安全地尝试索引5返回’test’。

希望这有帮助!

执行:

 int sa[4], r=0, t=0; String oneLine = "123;456;789;999;"; for (int i=0; i < oneLine.length(); i++) { if(oneLine.charAt(i) == ';') { sa[t] = oneLine.substring(r, i).toInt(); r=(i+1); t++; } } 

结果:

  // sa[0] = 123 // sa[1] = 456 // sa[2] = 789 // sa[3] = 999 

对于内存的动态分配,您需要使用malloc,即:

 String returnvalue[splitcount]; for(int i=0; i< splitcount; i++) { String returnvalue[i] = malloc(maxsizeofstring * sizeof(char)); } 

您还需要最大字符串长度。

基于分隔符拆分字符串的C方法是使用strtok (或strtok_r )。 另见这个问题。

我认为你的想法是一个很好的起点。 这是我使用的代码(用以太网屏蔽解析HTTP GET REST请求)。

我的想法是使用while循环和lastIndexOf并将字符串存储到一个数组中(但你可以做其他事情)。

“request”是你要解析的字符串(对我而言,它被称为请求,因为它是……)。

  int goOn = 1; int count = -1; int pos1; int pos2 = request.length(); while( goOn == 1 ) { pos1 = request.lastIndexOf("/", pos2); pos2 = request.lastIndexOf("/", pos1 - 1); if( pos2 <= 0 ) goOn = 0; String tmp = request.substring(pos2 + 1, pos1); count++; params[count] = tmp; // Serial.println( params[count] ); if( goOn != 1) break; } // At the end you can know how many items the array will have: count + 1 ! 

我成功地使用了这个代码,但是当我尝试打印params [x]时,我认为它们是一个编码问题...我是一个初学者,所以我不掌握字符与字符串......

希望能帮助到你。