-1

How do I combine both string and array in a string variable in Arduino's C/C++ dialect? I tried to run below lines but doesnt work.

int j = 0;
String value1[] = {0,1,2,3};

String httpRequestData = "&value1=" + value1[i] + "";
//then number of j++




 
Hans Lub
  • 5,513
  • 1
  • 23
  • 43
ssDev
  • 31
  • 6

1 Answers1

0

You need to initialize your string before concatenate different types.

More details here https://www.arduino.cc/en/Tutorial/StringAdditionOperator

int j = 0;
int value1[] = {0,1,2,3};

String httpRequestData = String("&value1=");
httpRequestData += value1[i];
Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • `"&value1=" + value1[i]` doesn't do what you think it does and it is wrong. – bolov Jul 27 '20 at 06:41
  • @bolov , I read the document of String in Arduino doc. I found that the String class has the capability to manage also integers. I've searched for String because I said that is not correct to use `sprintf(buff,"%d",value[i])` ... but now I cannot try if my concern is true! Elsewhere I think the correct way should be: `sprintf(buff,"%d",value[i].toInt())` – Sir Jo Black Jul 27 '20 at 07:44
  • 1
    @bolov what this will do ? According to the Arduino doc this will concatenate. From the doc: `stringThree = stringOne + 123;` – Ôrel Jul 27 '20 at 09:24
  • @Ôrel `"&value1="` is a C string literal and `value[i]` is `int`. Maybe `int value1[]` is a typo in your answer. – bolov Jul 27 '20 at 13:20
  • @bolov this is not a typo, as httpRequestData is a String, the magic of C++ will to the conversion and convert then concatenate the both as `String` not a `char *` and an `int`, check the shared link. Not saying this is clean, but this is the arduino way to do – Ôrel Jul 28 '20 at 00:03
  • @Ôrel incorrect. `"&value1=" + value1[i]` is evaluated as a standalone sub-expression and is doing pointer arithmetics. – bolov Jul 28 '20 at 00:08
  • ok I update it to build a String and then append – Ôrel Jul 28 '20 at 09:08