-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++




 
3
  • 2
    Does this answer your question? std::string formatting like sprintf Commented Jul 27, 2020 at 6:29
  • 2
    Can you give detail, doesnt work is not very clear. Commented Jul 27, 2020 at 6:30
  • you could try CStringBuilder from my StreamLib Commented Jul 27, 2020 at 8:13

1 Answer 1

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];
Sign up to request clarification or add additional context in comments.

7 Comments

"&value1=" + value1[i] doesn't do what you think it does and it is wrong.
@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())
@bolov what this will do ? According to the Arduino doc this will concatenate. From the doc: stringThree = stringOne + 123;
@Ôrel "&value1=" is a C string literal and value[i] is int. Maybe int value1[] is a typo in your answer.
@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
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.