字符串分割到字符串数组 [英] Split String into String array

查看:197
本文介绍了字符串分割到字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编程的Arduino但今天我遇到一个问题,我不能用我有限的知识Ç解决玩耍。
这里是怎么一回事呢。
我创建,发送串行输入到Arduino(设备ID,命令,commandparameters)的PC应用程序。这Arduino的将通过RF该指令发送给其他的Arduino的。根据不同的设备ID正确的Arduino将执行命令。

要能够确定我想拆的,该字符串的设备ID。
这是我的问题,我知道如何在Java中做到这一点很容易(甚至不使用标准分流功能),但在C中,它是一个完全不同的故事。

可以任何你们告诉我如何得到这个工作?

感谢

  / *
      系列事件的例子     当新的串行数据到达时,该小品将它添加到一个字符串。
     当收到一个新行,环路输出字符串和
     清除。     一个很好的测试,这是有GPS接收器来试试吧
     发送出NMEA 0183句。     创建9 2011年5月
     由汤姆Igoe     这个例子code是在公共领域。     http://www.arduino.cc/en/Tutorial/SerialEvent     * /    字符串inputString; //一个字符串来保存传入的数据
    布尔stringComplete = FALSE; //字符串是否齐全
    的String [] receivedData;    无效设置(){
      //初始化串口:
      Serial.begin(9600);
      //储备200个字节的inputString:
      inputString.reserve(200);
    }    空隙环(){
      当一个换行符到达//打印字符串:
      如果(stringComplete){
        Serial.println(inputString);
        //清除字符串:
        inputString =;
        stringComplete = FALSE;
      }
    }    / *
      的serialEvent每当有新的数据进来的发生
     硬件序列RX。该程序的每个之间运行
     时间循环()中运行,因此,使用内循环延迟会延迟
     响应。数据的多个字节可用。
     * /
    无效的serialEvent(){
      而(Serial.available()){
        //获取新的字节:
        焦炭INCHAR =(char)的Serial.read();
        如果(INCHAR =='\\ n'){
          stringComplete = TRUE;
        }
        //它添加到inputString:
        如果(stringComplete == FALSE){
          inputString + = INCHAR;
        }
        //如果输入字符是一个换行符,设置一个标志
        //所以主循环可以做一些事情:
      }
    }    的String [] splitCommand(字符串文字,字符splitChar){
      INT splitCount = countSplitCharacters(文字,splitChar);
      字符串的returnValue [splitCount]
      INT指数= -1;
      INT索引2;      的for(int i = 0; I< splitCount - 1;我++){
        指数= text.indexOf(splitChar,索引+ 1);
        索引2 = text.indexOf(splitChar,索引+ 1);        如果(索引2℃,)索引2 = text.length() - 1;
        的returnValue [I] = text.substring(索引,索引2);
      }      返回的returnValue;
    }    诠释countSplitCharacters(字符串文字,字符splitChar){
     INT的returnValue = 0;
     INT指数= -1;     而(索引> -1){
       指数= text.indexOf(splitChar,索引+ 1);       如果(索引> -1)的returnValue + = 1;
     }     返回的returnValue;
    }


我已经决定我要去使用 strtok的功能。
我遇到了另一个问题了。

  SerialEvent.cpp:在功能无效splitCommand(字符串,字符):的serialEvent:68:错误:无法将'字符串'到'字符*'的参数'1'到'字符* strtok的(字符*,为const char *)'的serialEvent:68:错误:'空'不是在这个范围内声明    字符串inputString; //一个字符串来保存传入的数据    无效splitCommand(字符串文字,字符splitChar){
    串温度;
    INT指数= -1;
    INT索引2;    对于(TEMP =的strtok(文字,splitChar);温度;温度=的strtok(NULL,splitChar)){
      Serial.println(临时);
    }    的for(int i = 0;我3;;我++){
      Serial.println(命令[I]);
    }
}


解决方案

这是一个老问题,但我已经创造了一些片code的,可以帮助:

 字符串的getValue(字符串数据,字符分隔,INT指数)
{
  INT发现= 0;
  INT strIndex [] = {0,-1};
  INT maxIndex = data.length() - 1;  的for(int i = 0; I< = maxIndex和放大器;&安培;发现< =指数;我++){
    如果(data.charAt(我)== ||分隔符==我maxIndex){
        发现++;
        strIndex [0] = strIndex [1] +1;
        strIndex [1] =(ⅰ== maxIndex)? I + 1:I;
    }
  }  返回找到>指数? data.substring(strIndex [0],strIndex [1]):,;
}

该函数返回一个给定的指标在由predefined字符分隔一个字符串。例如:

 字符串分割=你好,这是一个分割测试;
字符串WORD3 =的getValue(分裂,'',2);
Serial.println(WORD3);

应打印是。您还可以使用索引0返回'HI'或安全试图指数重返5测试尝试。

希望这有助于!

I have been playing around with programming for arduino but today i've come across a problem that i can't solve with my very limited C knowledge. Here's how it goes. I'm creating a pc application that sends serial input to the arduino (deviceID, command, commandparameters). This arduino will transmit that command over RF to other arduino's. depending on the deviceID the correct arduino will perform the command.

To be able to determine the deviceID i want to split that string on the ",". this is my problem, i know how to do this easily in java (even by not using the standard split function), however in C it's a totally different story.

Can any of you guys tell me how to get this working?

thanks

    /*
      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 < 0) index2 = text.length() - 1;
        returnValue[i] = text.substring(index, index2);
      }

      return returnValue;
    }

    int countSplitCharacters(String text, char splitChar) {
     int returnValue = 0;
     int index = -1;

     while (index > -1) {
       index = text.indexOf(splitChar, index + 1);

       if(index > -1) returnValue+=1;
     }

     return returnValue;
    } 


I have decided I'm going to use the strtok function. I'm running into another problem now.

SerialEvent.cpp: In function 'void splitCommand(String, char)':

SerialEvent:68: error: cannot convert 'String' to 'char*' for argument '1' to 'char* strtok(char*, const char*)'

SerialEvent:68: error: 'null' was not declared in this scope

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

解决方案

This is an old question, but i have created some piece of code that may help:

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

This function returns a single string separated by a predefined character at a given index. For example:

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

Should print 'is'. You also can try with index 0 returning 'hi' or safely trying index 5 returning 'test'.

Hope this help!

这篇关于字符串分割到字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆