954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to clear a string

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 :-/

joydsouza90
Light Poster
25 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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 :-/

const char *str="SpS";
  cout << str;
  str = NULL;
SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

It's a character string? Just set the first character to be a terminator and it'll be treated as "".

name[0] = '\0';

If it's a C++ string you can call the clear method and that truncates the string to be "".

name.clear();
Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

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

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

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.)

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

im using turbo c++
and none of the methods that u guys have mentioned seem to work :'(


name[20] is a character array

joydsouza90
Light Poster
25 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

Please post the code you have so far...

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Okay then, what are you doing exactly?

Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 
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
char name[20];
   memset(name, 0, sizeof name);


Although typically clearing all elements of a string is unnecessary.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

thanx a lot !!!:icon_cheesygrin:
memset works .

joydsouza90
Light Poster
25 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

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 :)

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

where can i get the latest version of turbo c++

joydsouza90
Light Poster
25 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 
Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

Personally, anything with the word turbo c in makes me cringe, although admittedly the latest version is supposed to confirm to the standard, so it is actually ok to use.

But I'd recommend using dev-cpp. It's a small download at only 10MB and comes with loads of options, open-gl, native win32 GUI support for windows etc.

You can get it here:
http://www.bloodshed.net/devcpp.html

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I think the dev-cpp project is dead. If you use it then you won't get any updates or support unless you want to do it all yourself. Code::blocks is supposed to be the successor to it from what I hear.

Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 
The problem is unlikely to be with their code but rather ... you're using the outdated version of turbo c, which is probably very likely :)


So you claim that theoutdated versions of C/C++, like Turbo, never handled strings correctly? :icon_rolleyes: Or is it that after new versions came out, the older versions simply wore out and developed bugs?

If you're going to continue to slam older compilers, at least use reasons that make sense...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

So you claim that the outdated versions of C/C++, like Turbo, never handled strings correctly? :icon_rolleyes: Or is it that after new versions came out, the older versions simply wore out and developed bugs?

If you're going to continue to slam older compilers, at least use reasons that make sense...

A slant I took on this is that the std namespace didn't exist and as such standard C++ is not supported. That is, there was no std::string , so using standard strings in an outdated compiler may have proved problematic. YMMV.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Dave, at least you have a viable reason. Most other reasons I see given here is simply because it's old. Not a valid reason IMAO.

My preference would be to stop telling people "your compiler's old, so upgrade to [my personal favorite] compiler." If that's what they are using, let them use it unless it's really crap (like Digital Mars).

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You