Remove leading and trailing spaces from a string in C++
This post will discuss how to remove leading and trailing spaces from a string in C++. In other words, left trim and right trim a std::string.
1. Using string::erase
The idea is to get the index of the first and last non-whitespace character using the std::string::find_first_not_of and std::string::find_last_not_of function respectively. Then erase the substring consisting of whitespace characters with the string::erase function.
|
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 |
#include <iostream> #include <string> std::string& ltrim(std::string &str, std::string const &whitespace = " \r\n\t\v\f") { str.erase(0, str.find_first_not_of(whitespace)); return str; } std::string& rtrim(std::string &str, std::string const &whitespace = " \r\n\t\v\f") { str.erase(str.find_last_not_of(whitespace) + 1); return str; } std::string& trim(std::string &str, std::string const &whitespace=" \r\n\t\v\f") { return ltrim(rtrim(str, whitespace), whitespace); } int main() { std::string str = " Test String "; std::cout << "[" << trim(str) << "]" << std::endl; // [Test String] return 0; } |
Here’s an equivalent version using the std::find_if function.
|
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 &str) { auto it = std::find_if(str.begin(), str.end(), [](char &c) { return !std::iswspace(c); }); str.erase(str.begin(), it); return str; } std::string& rtrim(std::string &str) { auto it = std::find_if(str.rbegin(), str.rend(), [](char &c) { return !std::iswspace(c); }); str.erase(it.base(), str.end()); return str; } std::string& trim(std::string &str) { return ltrim(rtrim(str)); } int main() { std::string str = " Test String "; std::cout << "[" << trim(str) << "]" << std::endl; // [Test String] return 0; } |
2. Using string::substr
The idea here remains the same – find the index of the first and last character that isn’t whitespace using the std::string::find_first_not_of and std::string::find_last_not_of function respectively. Then return the substring between the two positions using the string::substr function. Note this results in a new string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> std::string trim(std::string const &str, std::string const &whitespace=" \r\n\t\v\f") { auto start = str.find_first_not_of(whitespace); auto end = str.find_last_not_of(whitespace); return str.substr(start, end - start + 1); } int main() { std::string str = " Test String\n\t "; std::cout << "[" << trim(str) << "]" << std::endl; // [Test String] return 0; } |
3. Using std::regex_replace
Another option to remove leading and trailing spaces from a string is using regular expressions. Starting with C++11, we can use the std::regex_replace function, as demonstrated below. Note that this results in a new 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 |
#include <iostream> #include <string> #include <regex> std::string ltrim(std::string const &str) { return std::regex_replace(str, std::regex("^\\s+"), ""); } std::string rtrim(std::string const &str) { return std::regex_replace(str, std::regex("\\s+$"), ""); } std::string trim(std::string const &str) { return ltrim(rtrim(str)); } int main() { std::string str = " Test String\n\t "; std::cout << "[" << trim(str) << "]" << std::endl; // [Test String] return 0; } |
4. Using Boost
If you’re already using the Boost library, consider using the boost string algorithm boost::algorithm::trim. This solution is short and elegant, and modifies the string in-place. Here’s an example of its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <string> #include <boost/algorithm/string/trim.hpp> int main() { std::string str = " Test String "; boost::algorithm::trim(str); std::cout << "[" << str << "]" << std::endl; // [Test String] return 0; } |
5. Using std::string_view
C++17 allows forming a string view of a character literal with the header <string_view>. After getting the string view, we can use the remove_prefix and remove_suffix functions to left trim and right trim the string. For example,
|
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 |
#include <iostream> #include <string> #include <string_view> std::string_view ltrim(std::string_view str, std::string const &whitespace = " \r\n\t\v\f") { const auto pos(str.find_first_not_of(whitespace)); str.remove_prefix(std::min(pos, str.length())); return str; } std::string_view rtrim(std::string_view str, std::string const &whitespace = " \r\n\t\v\f") { const auto pos(str.find_last_not_of(whitespace)); str.remove_suffix(std::min(str.length() - pos - 1, str.length())); return str; } std::string_view trim(std::string_view str, std::string const &whitespace = " \r\n\t\v\f") { return ltrim(rtrim(str, whitespace), whitespace); } int main() { std::string_view str = " Test String "; std::cout << "[" << trim(str) << "]" << std::endl; // [Test String] return 0; } |
That’s all about removing leading and trailing spaces from a string 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 :)