1

I am trying to execute some webbased calls, but my string keep "disappearing", and I am highly confused about why.

I can println a string directly, but if I have it in a variable, it will disappear :I

I tried googling around, but could not find a reason for it. Is it because of the curly brackets ? Is it a memory issue ?

I feel I really can't code on and juggle web calls, if I can't even handle regular string commands..

Code:

Serial.println(" - CONSOLE/STRING WEIRDNESS - ");
String message = "Testing time {0}";
Serial.println(message);
Serial.println(String(message));
Serial.println("Testing time {0}");
Serial.println(message.substring(3,6));

Response:

- CONSOLE/STRING WEIRDNESS - 
(nothing)
(nothing)
Testing time {0}
(nothing)

The code and the debugger

Update

This seems to happen whenever there is a number inside a string, it even happends with a simple string like this:

Code:

Serial.println(" - CONSOLE/STRING WEIRDNESS - ");
String message = "Testing time here is a number 0";
Serial.println(message);
Serial.println(String(message));
Serial.println("Testing time here is a number 0");
Serial.println(message.substring(3,6));

Response:

- CONSOLE/STRING WEIRDNESS - 
(nothing)
(nothing)
Testing time here is a number 0
(nothing)
  • Out of memory? Looks like the String constructor failed to allocate storage on the heap. – Mikael Patel Apr 3 '17 at 10:37
0

Give this article a read, it highlights how strings are actually stored in the Arduino memory, and explains how strings should be used.

https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/

  • Turned out it was string weirdness indeed. Thank you so much for this, I will be cautious with the String class from now on. – Nils Munch Apr 3 '17 at 12:02
  • Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. Feel free to add a more complete answer that directly answers the question. – Nick Gammon Apr 4 '17 at 1:47
1

The {0} denotes an extra parameter (which you want to print), it seems you just want to print the string without any variable 'value' so you can remove the {0}.

I'm not so familiar with using {0} in C++, but in C# {0} means argument number 0 (after the initial string), since this argument is not given, probably an empty string is created ... it would be more intuitive if the compiler give an error or warning (maybe it gives a warning and you can check for it).

Also converting a string to String() does not do anything but I think it was just for debugging/checking purposes.

Also in Arduino to save SRAM space, normally it is wise to add before each string, the F function like:

Serial.println(F("Some string to print"));

This will create a memory in Flash and will not be copied to SRAM which is normally quite scarce.

To print out arguments you can simply use multiple print commands (which does not add a new line) and the last part of the line with println, like:

Serial.print(F("Test time: ");
Serial.println(test_time);
  • I can see why "{0}" would cause weirdness, but the fact that just having a number in it, and it still disappears, really confuses me... i.imgur.com/EzNmxOU.png – Nils Munch Apr 3 '17 at 9:58
  • {0} is the argument number (I edited the answer). – Michel Keijzers Apr 3 '17 at 10:04
  • Roger that, but did you see my update about a pure string with an integer in it (no curly brackets), and that still fails. Can I really have no numeric characters in a string ? – Nils Munch Apr 3 '17 at 10:07
  • I don't have a compiler, but normally it should work like Serial.println("Test time {0}, test_time); where test_time is the variable (not sure if you have to cast it to a string, I don't think so. – Michel Keijzers Apr 3 '17 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.