c++ remove trailing whitespace

#include <algorithm>

int main()
{
    std::string str = "H e l l o";
    str.erase(remove(str.begin(), str.end(), ' '), str.end());
    std::cout << str; // Output Hello
    
    return 0;
}

4.71
7

                                    remove trailing whitespace

4.71 (7 Votes)
0
4.38
8
Charty 105 points

                                    #include &lt;algorithm&gt; 
#include &lt;cctype&gt;
#include &lt;locale&gt;

// trim from start (in place)
static inline void ltrim(std::string &amp;s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
        return !std::isspace(ch);
    }));
}

// trim from end (in place)
static inline void rtrim(std::string &amp;s) {
    s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
        return !std::isspace(ch);
    }).base(), s.end());
}

// trim from both ends (in place)
static inline void trim(std::string &amp;s) {
    ltrim(s);
    rtrim(s);
}

// trim from start (copying)
static inline std::string ltrim_copy(std::string s) {
    ltrim(s);
    return s;
}

// trim from end (copying)
static inline std::string rtrim_copy(std::string s) {
    rtrim(s);
    return s;
}

// trim from both ends (copying)
static inline std::string trim_copy(std::string s) {
    trim(s);
    return s;
}

4.38 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
removing space in a string in c++ how to remove white spaces in a string in cpp cpp remove spaces from string c++ trim white space remove all whitespace from string c++ remove all whitespaces from string c++ c++ function to remove whitespace from string string without whitespace c++ c++ remove string space remove blank spaces from string c++ how to remove trailing whitespace in c++ c++ strip spaces remove spaces from a string cpp trim whitespace cpp c++ remove whitespace from end of string delete blank space in c++ c++ remove trailing whitespace remove whitespaces from string c++ c++ std::getline remove white space how to remove whitespaces in C++ string how to remove whitespace c++ remove whitespace and comma from string cpp remove whitespace from string cpp is it possible to erase white space from end of string C++ is it possible to erase white space C++ trim whitespace c++ remove whitespace in string in c++ c++ delete whitespace from string c++ string.c_str trim spaces how to remove space in c++ string how to remove space in string in c++ c++ remove whitespace from beginning of string Trim Spaces cpp how to remove spaces from a string in c++ how to remove spaces in string c++ c++ trim spaces how to clean white space from the string in c++ remove whitespace from sring c++ remove space c++ trim whitespace or lineending c++ remove trailing spaces c++ strip trailing spaces word c++ strip spaces word c++ how to delete trailing spaces in c++ c++ string without whitespaces removing spaces from a string in c++ c++ remove whitespace from multiline string how to strip spaces in c++ trim whitespace string c++ remove space from string in c++ c++ trim whitespace from end of string trim spaces in c++ how to remove white spaces c++ c++ string delete spaces remove white spaces std string c++ cpp whitespace removal how to remove trailing spaces in c++ string cppreference how to remove trailing spaces in c++ string delete white spaces from a string c++ remove whitespace std::string C++11 remove whitespace std::string remove space from string cpp trim spaces c++ remove all whitespace from string cpp stl C++ program to remove spaces in a string remove doublicate whitespace from string c++ ignore whitespace in string c++ trim whitespace in string c++ how to remove spaces from string cpp remove whitespace at the end cpp remove trailing whitespace cpp remove spaces from stirng c++ how to delete whitespace in a string C++ how to remove spaces from string in c++ trim whitespaces in cpp get rid of whitespace c++ c++ remove string spaces how to remove space from string c++ remove trailing spaces in string c++ cpp remove whitespace from string how to remove all whitespace from a string in c++ how to eliminate space in a string c++ whitespace remover from c++ code remove trailing space cpp c++ function to strip all whitespaces from string how to strip a line of whitespace c++ remove spaces on a string c++ remove spaces ona string c++ how to remove whitespace in c++ strip white spaces c++ how to clear whitespace from c++ string how to clear whitespace from c++ code remove space cpp c++ strip trailing whitespace remove space in string c++ c++ strip whitespace how to remove white spaces in a string in c++ remove trailing spaces in c++ C++ strip inner whitespaces remove white spaces from string in cpp how to remove space from string cpp c++ remove all whitespace from string std string remove whitespace c++ how to remove whitespace after comma c++ how to remove whitespace at th eend c++ remove whitespace from code remove white space from a string in c++ delete all whitespace in a string c++ c++ remove space from string remove spaces c++ remove white space from a string c++ how to ignore whitespace in c++ trim whitespace from string in c++ remove space string c++ c++ stl removing trailing spaces get rid of all white space c++\ c++ remove whitespace how to remove useless thing from string cpp strip whitespace from string c++ cpp string trim whitespace c++ begin string with whitespace string remove whitespace c++ remove leading and trailing spaces in c++ remove empty chars from string c++ find all spaces in std::stirng std function that trims front of space in string C std function that trims front of space in string c++ delete spaces from string c++ replace spaces with nothing remove space from string c++ remove spaces from a string c++ c++ get rid of whitespace in string C++ trim c++ erase white space c++ string remove whitespace how to remove spaces from string c++ remove spaces from string ka how to erase blank c++ trim c++. remove whitespace from buffer c++ c++ remove empty space from string strip string c++ c++ string copy without spaces c++ how to remove spaces from a string cppreference trim string how to trim string to remove spaces in c++ how to cut space in c++ c++ remove space in string whitespace character c++ remove spaces from string c++ clean spaces in string c++ std::string remove spaces at end remove duplicate characters in a string c++ stl removing all whitespaces from string c++ cpp remove whitespace in string cpp remove whitespace int string how to delete spaces from a string c++ c++ remove backslash from string trim() c++ remove word from string c++ remove empty spaces from string c++ how t o remove space from string in C++ trim in C++ clean space char * str c how to get rid of space between word in c++ ignore spaces and charchters in string c++ c++ trim string c++ remove numbers and space from string trim string c++ strip string cpp remove white space usign cpp remove white space from string in c++ how to delete a blank space c++ delete all whitespace from a stringstream c++ cpp replace spaces cpp replace paces remove whitespace from beginning and end of string c++ remove whitespace c++ remove whitespace from string c++ remove trailing whitespace c++ how to remove empty space in C++ c++ code to remove spaces from a string removing whitespace from string c++ strip spaces of string c++ c++ string remove spaces remove spaces at the start of a string cpp remove all spaces from a string c++ c++ remove spaces from string c++ remove whitespace from string
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source