将字符串拆分为字符串数组 [英] Split String into String array

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

问题描述

我一直在为 arduino 编程,但今天我遇到了一个问题,我无法用我非常有限的 C 知识解决这个问题.这是怎么回事.我正在创建一个将串行输入发送到 arduino(设备 ID、命令、命令参数)的 pc 应用程序.这个 arduino 将通过 RF 将该命令传输到其他 arduino.根据设备 ID,正确的 arduino 将执行命令.

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.

为了能够确定设备ID,我想在,"上拆分该字符串.这是我的问题,我知道如何在 Java 中轻松完成此操作(即使不使用标准 split 函数),但在 C 中则完全不同.

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?

谢谢

/*
  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;
} 


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


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

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

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

SerialEvent:68: 错误:无法将参数 '1' 的 'String' 转换为 'char*' 到 'char* strtok(c​​har*, const char*)'

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

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

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

应该打印'is'.您也可以尝试使用索引 0 返回 'hi' 或安全地尝试使用索引 5 返回 'test'.

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

希望对您有所帮助!

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

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