How to trim a string in C++
In this tutorial, we will discuss how to trim leading and trailing white spaces from a string in C++
Trim string with built-in methods in C++
We use inbuilt “find_first_not_of( )” and “find_last_not_of( )” functions to find the indices of the non white spaces and trim the white spaces around the given string.
Let’s see the code:
// C++ program to remove white spaces in a given string #include <bits/stdc++.h> using namespace std; string toRemove = " \n\r\t\f\v"; // all types of possible spaces to remove string leftTrim(const string &s){ auto start = s.find_first_not_of(toRemove); // finding the index just after white spaces return (start == string::npos) ? "" : s.substr(start); // removed leading white spaces } string rightTrim(const string &s){ auto end = s.find_last_not_of(toRemove); // finding the index just before white spaces return (end == string::npos) ? "" : s.substr(0, end + 1); // removed trailing white spaces } string trim(const string &s) { // first trimming the left white space i.e. leading white spaces and next trimming the right white spaces i.e. trailing white spaces return rightTrim(leftTrim(s)); } int main() { string s = " \tThis_is_testing\r\n "; cout << "The string before trimming: " << "BEGIN:" << s << ":END"; cout << "\nThe string after left trimming: " << "BEGIN:" << leftTrim(s) << ":END"; cout << "\nThe string after right trimming: " << "BEGIN:" << rightTrim(s) << ":END"; cout << "\nThe string after trimming from both sides: " << "BEGIN:" << trim(s) << ":END" << endl; return 0; }
Output:
The string before trimmming: BEGIN: This_is_testing :END The string after left trimming: BEGIN:This_is_testing :END The string after right trimming: BEGIN: This_is_testing:END The string after trimming from both sides: BEGIN:This_is_testing:END
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Trim string using boost
To trim a string we use an external library known as “boost”. Boost provides free peer-reviewed portable C++ source libraries. You should download the library from the boost website and add it to the “include” folder on your computer.
The “boost” library has 3 trim functions namely trim_left(), trim_right(), trim().
Let’s see the code:
// C++ program to remove white spaces in a given string #include <iostream> #include <boost/algorithm/string.hpp> using namespace std; using namespace boost::algorithm; int main() { string s1 = " \tThis_is_testing\r\n "; string s2 = s1, s3 = s1; cout << "The string before trimming:" << " BEGIN:" << s1 << ":END"; trim_left(s1); // trimming left white spaces of a string cout << "\nThe string after left trimming:" << " BEGIN:" << s1 << ":END"; trim_right(s2); // trimming right white spaces of a string cout << "\nThe string after right trimming:" << " BEGIN:" << s2 << ":END"; trim(s3); // // trimming both sides white spaces of a string cout << "\nThe string after trimming from both sides:" << " BEGIN:" << s3 << ":END" << endl; return 0; }
Output:
The string before trimmming: BEGIN: This_is_testing :END The string after left trimming: BEGIN:This_is_testing :END The string after right trimming: BEGIN: This_is_testing:END The string after trimming from both sides: BEGIN:This_is_testing:END
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Also read: Trim Strings – Boost String Algorithm in C++
Leave a Reply