CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2003
    Location
    china
    Posts
    53

    Unhappy how to delete ' ' in the string

    for example
    there is a string "the box is blue"
    how to change the string into "theboxisblue"
    or there is a string "the?box?is?blue"
    how to change the string into "theboxisblue"

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    When you say "string", you need to specify exactly what type of string you are talking about.

    std::string?

    an array of char?

    CString?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    You can use std::remove() algorithm to remove blanks
    (or some other character) from a NULL terminated c-style
    string, or from a std::string as shown below. For std::string
    you need to call its erase() member function also.

    Code:
    #include <algorithm> // for std::remove
    #include <string>    // for std::string
    
    int main()
    {
        char str1[] = "the box is blue";
        std::remove(str1,str1+strlen(str1)+1,' ');
    
        std::string str2 = "the?box?is?blue";
        str2.erase(std::remove(str2.begin(),str2.end(),'?') , str2.end() );
    
        return 0;
    }

  4. #4
    Join Date
    Jun 2003
    Posts
    16
    If u are using CString then following code will help u

    CString ss = "the?box?is?blue";
    ss.Replace("?","");

    In this "Replace" function the
    1st parameter is "What u want to replace in the string and
    2nd parameter is "With which u want to replace".

    if the string is like
    ss = "the box is blue";
    then replace will be like
    ss.Replace(" ","");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured