943,544 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12641
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 27th, 2007
0

How to clear a string

Expand 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
joydsouza90 is offline Offline
25 posts
since Dec 2006
Aug 27th, 2007
0

Re: 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
c++ Syntax (Toggle Plain Text)
  1. const char *str="SpS";
  2. cout << str;
  3. str = NULL;
Last edited by SpS; Aug 27th, 2007 at 12:24 pm.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 27th, 2007
0

Re: How to clear a string

It's a character string? Just set the first character to be a terminator and it'll be treated as "".
C++ Syntax (Toggle Plain Text)
  1. name[0] = '\0';
If it's a C++ string you can call the clear method and that truncates the string to be "".
C++ Syntax (Toggle Plain Text)
  1. name.clear();
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 27th, 2007
0

Re: How to clear a string

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
CPP Syntax (Toggle Plain Text)
  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
CPP Syntax (Toggle Plain Text)
  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.)
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Aug 27th, 2007
0

Re: How to clear a string

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


name[20] is a character array
Reputation Points: 10
Solved Threads: 0
Light Poster
joydsouza90 is offline Offline
25 posts
since Dec 2006
Aug 27th, 2007
0

Re: How to clear a string

Please post the code you have so far...
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 27th, 2007
0

Re: How to clear a string

Okay then, what are you doing exactly?
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 27th, 2007
0

Re: 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
C++ Syntax (Toggle Plain Text)
  1. char name[20];
  2. memset(name, 0, sizeof name);
Although typically clearing all elements of a string is unnecessary.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 27th, 2007
0

Re: How to clear a string

thanx a lot !!!
memset works .
Reputation Points: 10
Solved Threads: 0
Light Poster
joydsouza90 is offline Offline
25 posts
since Dec 2006
Aug 27th, 2007
0

Re: How to clear a string

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help with my C++
Next Thread in C++ Forum Timeline: How to joint 2 int???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC