groundFungus:
Does this do what you want? The strings array has pointers to the parts.
char array[] = "123,456,789,abc,def,ghi";
char *strings[10];
char *ptr = NULL;
void setup()
{
Serial.begin(115200); //Serial.print(array);
byte index = 0;
ptr = strtok(array, ",");
while(ptr != NULL)
{
strings[index] = ptr;
index++;
ptr = strtok(NULL, ",");
} //Serial.println(index);
for(int n = 0; n < index; n++)
{
Serial.println(strings[n]);
}
Serial.print(" the second entry = ");
Serial.println(strings[1]);
}
void loop()
{
// put your main code here, to run repeatedly:
}
edit: changed to use OP input array
Yes, this does what I want but I have a String that I need to separate and put each piece in its own index.
So the String "abc,123,987" would turn into an array with abc at index 0, 123 at 1, and 987 at 2
Sorry for the confusion.
As for UKHeliBob's answer, it causes the incoming data to be truncated before I even do anything with it and it sometimes doesn't even show up.
"I have a project where I have some data come in and I then splice it at the commas."
Parsing Strings is fairly simple. If you have control over the way the data is sent, then that is a big help on sorting the data out on the receiving end. Use the forum Google search function in the upper right of this page to search for "parse String" and similar key words. You will probably find many previous project discussions and code showing various methods.
"If I receive "123,456,789,abc,def,ghi", I want the array to have "123" as the first entry, "456" as the second etc."
Some test code from way back where a comma delineated string of characters is saved as a String, then parsed and the parts saved as variables. several ways doing parsing, depending on how the initial data is packaged and sent.
//zoomkat 11-12-13 String capture and parsing
//from serial port input (via serial monitor)
//and print result out serial port
//copy test strings and use ctrl/v to paste in
//serial monitor if desired
// * is used as the data string delimiter
// , is used to delimit individual data
String readString; //main captured String
String angle; //data String
String fuel;
String speed1;
String altidude;
int ind1; // , locations
int ind2;
int ind3;
int ind4;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 11-12-13"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like 90,low,15.6,125*
//or 130,hi,7.2,389*
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
delay(3); //small delay to allow input buffer to fill
if (c == '*') {
//do stuff
Serial.println();
Serial.print("captured String is : ");
Serial.println(readString); //prints string to serial port out
ind1 = readString.indexOf(','); //finds location of first ,
angle = readString.substring(0, ind1); //captures first data String
ind2 = readString.indexOf(',', ind1+1 ); //finds location of second ,
fuel = readString.substring(ind1+1, ind2+1); //captures second data String
ind3 = readString.indexOf(',', ind2+1 );
speed1 = readString.substring(ind2+1, ind3+1);
ind4 = readString.indexOf(',', ind3+1 );
altidude = readString.substring(ind3+1); //captures remain part of data after last ,
Serial.print("angle = ");
Serial.println(angle);
Serial.print("fuel = ");
Serial.println(fuel);
Serial.print("speed = ");
Serial.println(speed1);
Serial.print("altidude = ");
Serial.println(altidude);
Serial.println();
Serial.println();
readString=""; //clears variable for new input
angle="";
fuel="";
speed1="";
altidude="";
}
else {
readString += c; //makes the string readString
}
}
}