Problem with String

Hello everyone i have a problem with this piece of code:

String first = HTTP_req;
String second = first.substring(24,35);

inside the abc variable there is the following string frist: 123,124,252,&abc
it changes dynamically so sometimes string first: 123,12,25,&abc
or string first: 0,0,0,&abc
I'd like to have numbers in each of the three variables that are separated by commas in the string.
for example
r = 123
g = 124
b = 252
please help

Please post your sketch. I don't see an 'abc' variable and I am having a hard time understanding what you want.

Yes, of course you can't use substring() to extract variable length fields from a String. You have probably already guessed the reason - substring() works with fixed length fields.

So, are you asking, how to extract variable length fields? That is called "parsing" the string, you can try Googling it. Basically you need to read the string looking for the ',' delimiters and then use strtok() or readbytesUntil() to get all the characters until the next ',' is found.

I try to use a function strtok() but I can't handle changing string to char. I have an error that says "cannot convert 'String' to 'char*' in initialization".

   String first = HTTP_req;
   String second = first.substring(24,35);
  char* a = second;
String first = HTTP_req;
   String second = first.substring(24,35);
   char copy[50];
   second.toCharArray(copy, 50);
   char pch = strtok (copy,",");
     while(pch != NULL){
        Serial.println(pch);
        pch = strtok(NULL, " ");
      }

I used this code but it not work properly

int commaIndex = myString.indexOf(',');
//  Search for the next comma just after the first
int secondCommaIndex = myString.indexOf(',', commaIndex + 1);

String firstValue = myString.substring(0, commaIndex);
String secondValue = myString.substring(commaIndex + 1, secondCommaIndex);
String thirdValue = myString.substring(secondCommaIndex + 1); // To the end of the string

int r = firstValue.toInt();
int g = secondValue.toInt();
int b = thirdValue.toInt();

or something like that.

I try to use a function strtok() but I can't handle changing string to char.

Is there a good reason why the data is in a String in the first place instead of a zero terminated array of chars (aka a C string) ?

Where do the characters that make up the String come from ?

I send data via the website with the use of Arduino Ethernet Shield.
I corrected the code but table “pch” it does not store separated values.

     if (StrContains(HTTP_req, "RGB")) {
     Serial.print("\n");
     String first = HTTP_req;
     String second = first.substring(24,36);
     Serial.println("Second is");
     Serial.println(second);
       Serial.println("Finish");
       char copy[20];
    Serial.println("copy is");
    second.toCharArray(copy, 20);
       for(int a = 0;a<20; a++){
       Serial.println(copy[a]);
    }
    Serial.println("finish");

     char* pch = strtok(copy,",");
      for(int b = 0;b<3; b++){
        Serial.println(pch[b]);

       }
   

   

     }

Serial print:

Second is
236,172,172,
Finish
copy is
2
3
6
,
1
7
2
,
1
7
2
,

finish
2
3
6

Please respond to reply #6.

I use post #5 code and it works !
I am so sorry for my stupidity. Thank you very much for help :slight_smile:
You are awesome!

programator120:
I use post #5 code and it works !
I am so sorry for my stupidity. Thank you very much for help :slight_smile:
You are awesome!

Shameless plug:

Select that add Karma

There, I added Karma for posting a Shameless plug for yourself. :slight_smile:

-jim lee