Trim a string in C++ – Remove leading and trailing spaces
This post will discuss how to remove leading and trailing spaces from a string in C++. In other words, trim a string in C++.
1. Using find_first_not_of() with find_last_not_of() function
We can use a combination of string’s find_first_not_of() and find_last_not_of() functions to remove leading and trailing spaces from a string in C++ by finding the index of the first and last non-whitespace character and pass the index to substr() functions to trim the string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> #include <string> #include <algorithm> const std::string WHITESPACE = " \n\r\t\f\v"; std::string ltrim(const std::string &s) { size_t start = s.find_first_not_of(WHITESPACE); return (start == std::string::npos) ? "" : s.substr(start); } std::string rtrim(const std::string &s) { size_t end = s.find_last_not_of(WHITESPACE); return (end == std::string::npos) ? "" : s.substr(0, end + 1); } std::string trim(const std::string &s) { return rtrim(ltrim(s)); } int main() { std::string s = "\n\tHello World \r\n"; std::cout << "START::" << trim(s) << "::END"; return 0; } |
2. Using std::find_if function
Another efficient solution is to use std::find_if to remove leading and trailing spaces, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> #include <string> #include <algorithm> std::string& ltrim(std::string &s) { auto it = std::find_if(s.begin(), s.end(), [](char c) { return !std::isspace<char>(c, std::locale::classic()); }); s.erase(s.begin(), it); return s; } std::string& rtrim(std::string &s) { auto it = std::find_if(s.rbegin(), s.rend(), [](char c) { return !std::isspace<char>(c, std::locale::classic()); }); s.erase(it.base(), s.end()); return s; } std::string& trim(std::string &s) { return ltrim(rtrim(s)); } int main() { std::string s = "\n\t Hello World "; std::cout << "START::" << trim(s) << "::END"; return 0; } |
3. Custom function
We can also write our own routine for this, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> #include <string> #include <algorithm> std::string trim(const std::string &s) { auto start = s.begin(); while (start != s.end() && std::isspace(*start)) { start++; } auto end = s.end(); do { end--; } while (std::distance(start, end) > 0 && std::isspace(*end)); return std::string(start, end + 1); } int main() { std::string s = "\n\t Hello World \r\n"; std::cout << "START::" << trim(s) << "::END"; return 0; } |
4. Using std::regex_replace function
With C++11, we can also use std::regex_replace for this. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <string> #include <regex> std::string ltrim(const std::string &s) { return std::regex_replace(s, std::regex("^\\s+"), std::string("")); } std::string rtrim(const std::string &s) { return std::regex_replace(s, std::regex("\\s+$"), std::string("")); } std::string trim(const std::string &s) { return ltrim(rtrim(s)); } int main() { std::string s = " \t\n Hello World "; std::cout << "START::" << trim(s) << "::END"; return 0; } |
5. Using Boost Library
Another good alternative is to use Boost Library. It provides trim_right(), trim_left() and trim() functions.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <string> #include <boost/algorithm/string/trim.hpp> int main() { std::string s = "\n\tHello World \r\n"; boost::algorithm::trim(s); std::cout << "START::" << s << "::END"; return 0; } |
That’s all about removing leading and trailing spaces in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)