| | |
How to clear a string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
•
•
How do you clear a string in a single statement ?
for egs. i want to clear name[20] of all its contents and leave it completely empty
Please help
c++ Syntax (Toggle Plain Text)
const char *str="SpS"; cout << str; str = NULL;
Last edited by SpS; Aug 27th, 2007 at 12:24 pm.
It's a character string? Just set the first character to be a terminator and it'll be treated as "".
If it's a C++ string you can call the clear method and that truncates the string to be "".
C++ Syntax (Toggle Plain Text)
name[0] = '\0';
C++ Syntax (Toggle Plain Text)
name.clear();
The truth does not change according to our ability to stomach it.
Are you talking about C++ strings or C-style char arrays? (I assume you're talking about char arrays, based on your post)
What do you mean by "completely empty"?
You can make a C++ string blank by doing this
for a C-style char array, you could do this
Or do you wish to iterate through every element and set each one to zero? (You can do that with a loop, although there's not much point.)
What do you mean by "completely empty"?
You can make a C++ string blank by doing this
CPP Syntax (Toggle Plain Text)
std::string name = "hello"; std::cout << name; //Assign 'name' a blank string name = ""; std::cout << name;
for a C-style char array, you could do this
CPP Syntax (Toggle Plain Text)
char name[20] = "fred"; std::cout << name; //If the first character is a null terminator, the string is 'empty'. name[0] = '\0' ; std::cout << name;
Or do you wish to iterate through every element and set each one to zero? (You can do that with a loop, although there's not much point.)
¿umop apisdn upside down? •
•
•
•
How do you clear a string in a single statement ?
for egs. i want to clear name[20] of all its contents and leave it completely empty
C++ Syntax (Toggle Plain Text)
char name[20]; memset(name, 0, sizeof name);
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
You should have posted the code for the others you have tried and the error messages or problems you encountered. The problem is unlikely to be with their code but rather how you typed it in or if you're using the outdated version of turbo c, which is probably very likely
Last edited by iamthwee; Aug 27th, 2007 at 2:26 pm.
![]() |
Similar Threads
- to clear a string (C++)
- Please help with program - STUCK (C)
- using stringstream for tokenizing a string (C++)
- String to disk with fstream (C++)
- file save load problems (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help with my C++
- Next Thread: How to joint 2 int???
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets







