Please help me with parsing a String delimited by a comma?

Hi,

I have a string "strData" that contains data "19f1ff, 2" where the first number is a hexadecimal RGB color code, and the second number is just a number.

How can I parse "strData" so that string variable "strRGBHex" = 19f1ff and string variable "strNumber" = 2?

I use the comma in between as a delimiter. Any example code that can parse "19f1ff, 2" into 2 string variables would be greatly appreciated. Thanks!

Hi, the function atoi is supported ( loads of online documentation ). include stdlib.h

use atoi to read the first value.
find position of comma, if a fixed size it can be hardcoded ( if the hex value is binary data of a constant size )
then use itoa again at the new position.

also itoa is useful sometimes.

@pYro_65

noobtoarduino:
Hi,

I have a string "strData" that contains data "19f1ff, 2" where the first number is a hexadecimal RGB color code, and the second number is just a number.

How can I parse "strData" so that string variable "strRGBHex" = 19f1ff and string variable "strNumber" = 2?

I use the comma in between as a delimiter. Any example code that can parse "19f1ff, 2" into 2 string variables would be greatly appreciated. Thanks!

  char strData[] =  "19f1ff, 2";
  String newData = strtok(", ", strData);

  String strRGBHex = newData;
  newData  = strtok(NULL, ", ");
  String strNumber = newData;

Going off completely from memory... but strtok function should do what you need.

Going off completely from memory

Your memory is failing you, then.

OP: It's important for you to clarify if you are referring to a string (lower case s - a NULL terminated array of characters) as mentioned in the body of your post, or a String (upper case s - a class that wraps char array processing) as mentioned in your subject.

The String class has an indexOf() method that will tell you where the comma is, and a method to extract a substring. You can use the substring method to extract the two sub string (String objects) after you know where the comma is.

If you are referring to string, strtok() is the proper method. It returns a char *, not a String, so all the processing is done using char arrays or char pointers.

PaulS,

I think you nailed exactly why I was confused to begin with. At first, I tried using lower-case string. But I got confused and couldn't get it to work, and starting using upper-case String.

Currently, this is my code.

String strData;
String strRGBHex;
String strNumber

strData = "19f1ff, 2";
int nComma = strData.indexOf(',');

Serial.print(strData);
Serial.print(nComma);

Can you show me how to parse strData (an upper-case String) such that strRGBHex and strNumber is assigned 19f1ff and 2 respectively?

String strData;
String strRGBHex;
String strNumber

strData = "19f1ff, 2";
int nComma = strData.indexOf(',');

// Get the characters up to, but not including the comma
strRGBHex = strData.substring(0, nComma-1);

// Get the characters after the comma
strNumber = strData.substring(nComma+1);

Serial.print(strData);
Serial.print(nComma);

Serial.print() statements are useful, but something like this is even better:

Serial.print("strData: [");
Serial.print(strData);
Serial.println("]");
Serial.print("Comma position: ");
Serial.println(nComma);

Ah thanks so much Paul! I get it now, and your help is very much appreciated. :slight_smile:

I get how to parse a String, and would like to learn the other way you mentioned using strtok.

Let's say I have a character array cArray that contains the same data as String strData in our last example.

char cArray[9]="19f1ff, 2";

How would I use strtok to break the character array into 2 strings?

While not specific to your example, it should provide you the necessary details for using strtok(). A useful exercise would be to attempt to adapt it to your example.

Thanks jraskell. That link was very helpful, and I think I got what I needed!