It depends on what you mean by "empty". If you just want a zero-length string, then your example will work.

This will also work:

buffer[0] = '\0';

If you want to zero the entire contents of the string, you can do it this way:

memset(buffer,0,strlen(buffer));

but this will only work for zeroing up to the first NULL character.

If the string is a static array, you can use:

memset(buffer,0,sizeof(buffer));
Answer from Mysticial on Stack Overflow
🌐
Quora
quora.com › How-can-I-empty-a-String-in-Java
How to empty a String in Java - Quora
Answer (1 of 11): You can't. Strings are immutable. You can just reference a new string object that has 0 characters. So if you have the string [code] String whoIsAwesome = "you"; [/code] you can assign a new String object to your variable like ...
🌐
Cplusplus
cplusplus.com › reference › string › string › clear
std::string::clear
Erases the contents of the string, which becomes an empty string (with a length of 0 characters). ... This program repeats every line introduced by the user until a line contains a dot ('.'). Every newline character ('\n') triggers the repetition of the line and the clearing of the current string content. Unspecified, but generally constant. Any iterators, pointers and references related to this object may be invalidated.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Clearing a String - Programming - Arduino Forum
January 27, 2011 - Hey Guys, This is probably an easy question for most of you guys. What is the best way to clear a String value created by the String object? All I am doing is filling up a String with values but then I need an easy way…
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › stdstringclear-in-cpp
std::string::clear in C++ - GeeksforGeeks
July 23, 2025 - void string ::clear () - Removes all characters (makes string empty) - Doesn't throw any error - Receives no parameters and returns nothing ... // CPP code to illustrate // clear() function #include <iostream> #include <string> using namespace ...
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 114330-clear-string.html
clear a string
I just want to clean it and fill it up with null characters. ... mystring[0] = '\0'; That will effectively clear your string. There is no need to fill it with null characters.
🌐
Reddit
reddit.com › r/cpp › clear a string after declaration?
r/cpp on Reddit: Clear a string after declaration?
October 18, 2023 -

My senior had a fit when he saw that in my code there were several initialization like

std::string str = "";

He told me to use

std::string str; str.clear();

He said using 1st method caused some "stl corruption crash" thing in production earlier, and didn't explain further, just said to used 2nd method always.

Can anyone explain how 1st method can lead to crash?

Find elsewhere
🌐
Educative
educative.io › answers › what-is-stdbasicstringclear-in-c-cpp
What is std::basic_string::clear in C/C++?
The first string contains a phrase and, using the clear() function, the string is emptied. Upon printing the same string after applying the clear() function, an empty string is returned.
🌐
Wikipedia
en.wikipedia.org › wiki › Empty_string
Empty string - Wikipedia
January 16, 2026 - In formal language theory, the empty string, also known as the empty word or null string, is the unique string of length zero. Formally, a string is a finite, ordered sequence of characters such as letters, digits or spaces. The empty string is the special case where the sequence has length ...
🌐
TutorialsPoint
tutorialspoint.com › cpp_standard_library › cpp_string_clear.htm
C++ String Library - clear
It erases the contents of the string, which becomes an empty string. Following is the declaration for std::string::clear. ... In below example for std::string::clear. #include <iostream> #include <string> int main () { char c; std::string str; std::cout << "Please type some lines of text. Enter a start (*) to finish:\n"; do { c = std::cin.get(); str += c; if (c=='\n') { std::cout << str; str.clear(); } } while (c!='*'); return 0; }
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › operations-required-to-make-the-string-empty
Operations required to make the string empty - GeeksforGeeks
September 15, 2022 - Given a string str, the task is to make the string empty with the given operation. In a single operation, you can pick some characters of the string (each of the picked characters should have the same frequency) and remove them from the string.
🌐
Quora
quora.com › How-do-you-clear-a-string-variable-in-the-C-programming-language
How to clear a string variable in the C programming language - Quora
Answer (1 of 12): From the answers here you can see that simple things are lessons in pain in C. So many primitive and low-level ways to do a single logical high-level requirement. At the logical level, we should not think about memory at all, just set a string reference to new strings. However, ...
🌐
Rosetta Code
rosettacode.org › wiki › Empty_string
Empty string - Rosetta Code
1 week ago - By default, strings are initially empty so the assignment is not necessary. Another way to check for an empty string is to use the LEN function. 10 LET A$ = " 40 IF LEN (A$) = 0 THEN PRINT "THE STRING IS EMPTY" 50 IF LEN (A$) THEN PRINT "THE STRING IS NOT EMPTY"
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How to clear a string - Programming - Arduino Forum
November 30, 2021 - Here is my code: · String data (""); char data1= '7'; int see = 0; void setup() { Serial.begin(9600); } void loop(){ if (Serial.available()) { if (see<7) { data1 = Serial.read(); data += ...