0

Currently I convert the characters in char received[20] to string using String randomString(received) and I am able to display it using Serial.println(randomString). After displaying the string, I would like to know the easiest way to clear out its contents.

2
1

In C and C++ a null terminated array of characters is a string (although not a String). Many routines for reading input terminate the result with a null as does an assignment like"

char foo[] = "This is a string.";

All you would need to do to "clear" this string is to assign a null to the first character. For example:

foo[0] = '\0';
2
  • This clears only the first byte. With = { 0 }; everything is cleared. – ott-- Jan 11 '16 at 14:29
  • For C (null terminated) strings, if the first character is a null, then the string is empty or clear. Functionally that solves the problem, but if there are security concerns then you are right that the whole string should be cleared. – dlu Jan 11 '16 at 15:15
0

First version with conversion from char[] to Arduino String class:

char received[20];
...
// Some code that assigns received
...
String randomString(received); 
Serial.println(randomString);

// Clear string
randomString = "";

Second version without the Arduino String class:

char received[20];
...
// Some code that assigns received
...
Serial.println(received);

// Empty received buffer
received[0] = 0;

And third version where the buffer is cleared:

char received[20];
...
// Some code that assigns received
...
Serial.println(received);

// Clear received buffer
memset(received, 0, sizeof(received)); 

Cheers!

-1

Although an old question, I thought I would chime in. If I took the time to understand the question properly, I believe you wanted to clear the contents of the String and not the char array. This may help those who wanted to empty a string for other reasons.

String randomString(received);
Serial.println(randomString);
randomString.clear(); // This will clear the string

It should be noted that for outputing, there was no need to convert to string first if that was all that was required, because println will prefer a char array anyway.

Again String has a .clear() method for those who reached this page via web search.

Update: It has been pointed out thanks to @VE7JRO in the comments, that the clear methods availablity is not universal. The ESP devices with Arduino compatibile core libraries do have it, as may others. But AVR devices such as the UNO do not. Which I have confirmed.

A revised version to suit all cases where the .clear method is unavailable.

String randomString(received);
Serial.println(randomString);
randomString = ""; // This will clear the string
New contributor
voidPointer is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • Arduino IDE 1.8.9 says this about the clear() function "'class String' has no member named 'clear'". What version of the IDE / other software are you using? – VE7JRO yesterday
  • @VE7RJO Using platformio, it shows the method. Which I have alreay used in code. It may depend on whether using AVR or the ESP's but it is certainly present on the ESP8266. – voidPointer yesterday
  • The OP's question "title" mentions "Arduino", which as far as I know is not an ESP8266. Perhaps another Arduino user with a newer version of the IDE could confirm / deny whether this woks or not. – VE7JRO yesterday
  • @VE7JRO Have revised after testing. Thanks. – voidPointer yesterday

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.