Substring() problem

Hello everyone,
I am trying to extract certain data from a string. When i print the extracted data using the serial monitor it displays the right data. But, if i assign the extracted data to a variable and print that variable using the serial monitor i get all wrong data. Does anyone know where did it go wrong.

String qty,price,landed,delv;
String url = "2,40.500,18.000,1.500";
void setup() {
  Serial.begin(9600);
}

void loop() {
//locate the "," 
int firstcom = url.indexOf(","); 
int secondcom =  url.indexOf(",",firstcom+1);
int thirdcom = url.indexOf(",",secondcom+1);

//Assign extracted data to variables
qty = url.substring(0,firstcom);
price = url.substring((2,8));
landed = url.substring((9,15));
delv = url.substring(thirdcom+1);

//Print the index of the ","
Serial.println(firstcom);
Serial.println(secondcom);
Serial.println(thirdcom);

//print the extracted data...it prints right
Serial.println( url.substring(0,firstcom));
Serial.println( url.substring(firstcom+1,secondcom));
Serial.println( url.substring(secondcom+1,thirdcom));
Serial.println( url.substring(thirdcom+1));

//print the variables that contains extracted data...it prints wrong
Serial.println(qty);
Serial.println(price) ;
Serial.println(landed) ;
Serial.println(delv) ;
delay(3000);

}
price = url.substring((2,8));

Remove the extra brackets

price = url.substring(2,8);

Thank you very much, that too care of it.
the code i posted was a function of a large e-commerce project.
it seems i need to have my eyes checked :slight_smile:
thanks again

Which Arduino board are you using ?

I am sure that you will be aware that the general advice is not to use Strings on boards with limited memory

Yes i am aware of memory limitation
i am using ESP8266

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.