Thread: Clearing a String

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    59

    Clearing a String

    How can I clear a string? and not just by making all the characters spaces.

    Thanks

  2. #2
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Wink Re: Clearing a String

    Originally posted by Jperensky
    How can I clear a string? and not just by making all the characters spaces.

    Thanks
    Do you mean like the whole screen?? If so then clr() will do it!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Or,
    Code:
    std::string str = "Now is the time...";
    std::cout << str << std:endl;
    str = " ";
    This assumes, of course, that your string variable is named 'str'. Now, if it's named 'Rover'...

    Note that your compiler will probably insist that you put a 'space' character between the quotes.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Assuming this is a std::string you can simply say:

    str.erase();

    If it's a C string you'll have to define "empty" better, for most uses doing this will cause the string to become empty:

    str[0] = '\0';

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    27

    string??

    Hi,
    Do you mean a string std::string or an array of char's?

    To clear an array of char's so that C sees an empty string just make the first element 0(zero).


    char mystr[20] = "this is a test";
    printf("\n%s",mystr); //prints this is a test

    mystr[0] = 0;

    printf("\n%s",mystr); //prints only the '/n'

    Hope this helps.
    Pappy
    You learn something new everyday.

  6. #6
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    its generally better to assign the element of the string to NULL rather than zero
    Monday - what a way to spend a seventh of your life

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >its generally better to assign the element of the string to NULL rather than zero
    If you mean the nul character '\0' then I agree. If you mean the NULL macro defined in cstdio I would like to know why you think so.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by iain
    its generally better to assign the element of the string to NULL rather than zero
    Look at the definition of NULL (_null.h) and you'll be surprised .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    59

    Linux...

    I am using an array of chars not std::string and I tried _null.h but I am programming this to work under Linux so that isn't working.

    I am looping threw a text file char by char storing each char to the next available spot (or cell) in the array once it reads a \n char it stops and I can process the string (array of chars) after that I need to "reset the string." The problem I am having is I can't get the string to clear. So when I rewrite over the char array if anyline previously stored was longer than anyone after I have the extra char(s) after the string (line) I am working with.

    *Example*

    The text file contains:
    this is line one
    and line 2
    also 3

    I am getting this when I print it to screen:
    this is line one
    and line 2 one
    also 3ne 2 one

    Anyone have a solution?

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    There is a function called strset() defined in string.h.
    e.g.#include<string.h>
    strset(samplestring,0);
    This will set all characters in the string samplestring to the NULL character with the ASCII value 0.

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Linux...

    Originally posted by Jperensky
    I am using an array of chars not std::string and I tried _null.h but I am programming this to work under Linux so that isn't working.

    I am looping threw a text file char by char storing each char to the next available spot (or cell) in the array once it reads a \n char it stops and I can process the string (array of chars) after that I need to "reset the string." The problem I am having is I can't get the string to clear. So when I rewrite over the char array if anyline previously stored was longer than anyone after I have the extra char(s) after the string (line) I am working with.

    *Example*

    The text file contains:
    this is line one
    and line 2
    also 3

    I am getting this when I print it to screen:
    this is line one
    and line 2 one
    also 3ne 2 one

    Anyone have a solution?
    You copy the string into the buffer one char at a time?
    That explains it. After you have copied all characters, add a '\0' character last. This is a NULL terminator, meaning the string ends there (not showing up the rest of the junk from the previous string).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    59

    well...

    When I append a '\0' char to the end of the array all I can see is the last char in the string... it is the correct char though.

    Source:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <string.h>
    
    
    
    void main()
    
    {
    
          ifstream OpenFile("text.txt");
          char ch, temp_str[50];
          int cord = 0, reset = 0;
          while(!OpenFile.eof())
    
          {
    
                OpenFile.get(ch);
                
                   if (ch == '\n') {
                      cord++;
                      temp_str[cord] = '\0';
                      cout<<temp_str<<"\n";                  
                   }                 
                   else {
                      temp_str[cord] = ch;
                      cord++;   
                   }  
                   reset = 0;
                   cord = 0;
    
          }
    
          OpenFile.close();
          
          cout<<temp_str;
    
    }

  13. #13
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: well...

    You set the index to 0 in every loop, so you will never get further than the first element.
    Originally posted by Jperensky
    Code:
    OpenFile.get(ch);
                
    if (ch == '\n') 
    {
       cord++;
       temp_str[cord] = '\0';
       cout<<temp_str<<"\n";                  
    }                 
    else 
    {
       temp_str[cord] = ch;
       cord++;   
    }  
    
    reset = 0;
    cord = 0;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    59

    What do you mean?

    You said I set the index to '\0'. I set the last char in the string to '\0' as I was instructed to do in a previous post. How should I do it?

  15. #15
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You should move the cord = 0; to the if statement:
    Code:
    if (ch == '\n') {
       cord++;
       temp_str[cord] = '\0';
       cout<<temp_str<<"\n";                  
       cord = 0;
    }                 
    else {
       temp_str[cord] = ch;
       cord++;   
    }  
    reset = 0;
    If you read a newline character you display the string and reset the index (cord) to 0. Now you can read the next line starting at the beginning of your string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM