How to clear a string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2006
Posts: 24
Reputation: joydsouza90 is an unknown quantity at this point 
Solved Threads: 0
joydsouza90 joydsouza90 is offline Offline
Newbie Poster

How to clear a string

 
0
  #1
Aug 27th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: How to clear a string

 
0
  #2
Aug 27th, 2007
Originally Posted by joydsouza90 View Post
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
  1. const char *str="SpS";
  2. cout << str;
  3. str = NULL;
Last edited by SpS; Aug 27th, 2007 at 12:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: How to clear a string

 
0
  #3
Aug 27th, 2007
It's a character string? Just set the first character to be a terminator and it'll be treated as "".
  1. name[0] = '\0';
If it's a C++ string you can call the clear method and that truncates the string to be "".
  1. name.clear();
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 492
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: How to clear a string

 
0
  #4
Aug 27th, 2007
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
  1. std::string name = "hello";
  2. std::cout << name;
  3.  
  4. //Assign 'name' a blank string
  5. name = "";
  6. std::cout << name;

for a C-style char array, you could do this
  1. char name[20] = "fred";
  2. std::cout << name;
  3.  
  4. //If the first character is a null terminator, the string is 'empty'.
  5. name[0] = '\0' ;
  6. 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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 24
Reputation: joydsouza90 is an unknown quantity at this point 
Solved Threads: 0
joydsouza90 joydsouza90 is offline Offline
Newbie Poster

Re: How to clear a string

 
0
  #5
Aug 27th, 2007
im using turbo c++
and none of the methods that u guys have mentioned seem to work


name[20] is a character array
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How to clear a string

 
0
  #6
Aug 27th, 2007
Please post the code you have so far...
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: How to clear a string

 
0
  #7
Aug 27th, 2007
Okay then, what are you doing exactly?
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,438
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to clear a string

 
0
  #8
Aug 27th, 2007
Originally Posted by joydsouza90 View Post
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
  1. char name[20];
  2. memset(name, 0, sizeof name);
Although typically clearing all elements of a string is unnecessary.
"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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 24
Reputation: joydsouza90 is an unknown quantity at this point 
Solved Threads: 0
joydsouza90 joydsouza90 is offline Offline
Newbie Poster

Re: How to clear a string

 
0
  #9
Aug 27th, 2007
thanx a lot !!!
memset works .
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How to clear a string

 
0
  #10
Aug 27th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC