Joining two string variables into one string variable

I have a program that generates random numbers ranging from 1-5 to two INT variables.
Then I convert them both to String variables.
Then I join them into one Major String Variable.

int port1;
int port2;
String value1;
String value2;
String all; //major string holder

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop() {

  // print a random number from 1 to 10
  port1 = random(1, 10);
  port2 = random(1, 10);

  value1 = String(port1 / 1.5, DEC);
  value2 = String(port2 / 1.5, DEC);
  all = value1 +  value2;
  
  Serial.println(all);
  delay(1500);
}

The output looks like this:

2.66666670004.0000000000

How can I make the output look like these?

2.6666667000 4.0000000000

I don't know how to separate with a space the two string variables that I joined in the major string variable.

all = value1 + " " + value2;

Regards,
Ray L.

Thank you ! It worked!

what if I am going to put words in the major string handler that is;

Value1 is: 2.6666667000 Value2 is: 4.0000000000

:slight_smile:

You didn't understand what the " " meant? What effect it had?

I tried this code:

  all =  "value1:" + value1 + " " + "value2:" + value2 + " ";

and gave me an error, I tried to compile it again, it works just fine now. Thanks again!

Is there any compelling reason to concatenate the strings before printing? Can you not just print them sequentially? Then you could drop the String library entirely and avoid the memory problems that it can cause.

The values are going to a web service, the programmer of the web server wants the data to be in only one string variable, there, he access that one variable for his usage.

Probably because the official documentation steers people towards Strings instead of strings, because they're "easier" - apparently debugging bizarre problems caused by memory fragmentation is easier than using char arrays and c string functions like strcat() and the like.

I have yet to need to resort to String - I use the technique of printing each "piece" to serial one at a time very widely (so I rarely have to concatenate strings - this also makes the F() macro more convenient), and when I do, there's strcat() - and as an added bonus, you're using fixed size buffers, so I know how much memory they're using and they're even accounted for in the post-compile memory numbers if they're globals.

urbanmiles:
The values are going to a web service, the programmer of the web server wants the data to be in only one string variable, there, he access that one variable for his usage.

So you're long term not going to send them over serial, but instead send it over some network connection?

All the more reason NOT to use String, since network libraries are often pretty memory hungry themselves.

urbanmiles:
The values are going to a web service, the programmer of the web server wants the data to be in only one string variable, there, he access that one variable for his usage.

If that's all you need, it's possible to do that using only character arrays.

Thanks guys, I realized your thoughts and considering now using string arrays, however I am new to arduino programming,, could give me a headstart from this point of the program, on how will I use the Cha array after I got the random values from ports 1 and 2?

int port1;
int port2;
String value1;
String value2;
String all; //major string holder

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop() {

  // print a random number from 1 to 10
  port1 = random(1, 10);
  port2 = random(1, 10);

//use port 1 and port 2 values in a character array

sprintf