Hello everyone, In this article we will see how string trimming can be done in C++. Let us first understand what is trimming and where it can be of importance.
Trim: Trimming is a string operation where we remove extra whitespaces from the start and end of a string and if some spaces are present in between the string those won’t be removed with the operation.
For e.g. “ Hello world “ will become “Hello world”.
Now where and how trimming a string can be useful?
Trimming is done in forms where user type some information which is then sent to servers. So if your email address is abc@xyz.com and you mistakenly typed a space at last that should be removed before sending it to the server.
C++ String Trim – 3 Ways to Trim String
Method 1: Writing Own Trim Function
We can write our function for trimming a string, we just have to identify the number of spaces present at beginning before any non-space character and same from the end. The function to do the same is illustrated below:
#include <bits/stdc++.h> using namespace std; string trimfnc(string str) { // Initialize considering no trimming required. int atstart = 0, atend = str.size()-1; // Spaces at start for(int i=0;i<str.size();i++) { if(str[i] != ' ') { atstart = i; break; } } // Spaced from end for(int i = str.size()-1;i>=0;i--) { if(str[i] != ' '){ atend = i; break; } } string fistr = ""; for(int i=atstart;i<=atend;i++) { fistr += str[i]; } return fistr; } int main() { string str; getline(cin, str); cout<<trimfnc(str); }
Output:
This method works well for normal cases but there can be times in real world application where there are many different encodings will be followed there it may feel, so it’s better to go with standard functions provided by c++ libraries.
The above method will only remove whitespaces (normal spaces) but there can be various different whitespaces as below:
\t: Horizontal tab
\v: vertical tab
\n: new line
\r: carriage return
\f: form feed
These all look like spaces but are different so these need also need to be removed.
Mehod 2: C++ Standard Library
#include <bits/stdc++.h> using namespace std; string trimfnc(string str) { const char* typeOfWhitespaces = " \t\n\r\f\v"; str.erase(str.find_last_not_of(typeOfWhitespaces) + 1); str.erase(0,str.find_first_not_of(typeOfWhitespaces)); return str; } int main() { string str; getline(cin, str); str = trimfnc(str); cout<<str; }
Here we are using the find_last_not_of and find_first_not_of functions from the string library. It takes in an argument where we have specified the characters which we have to exclude. It will return us the first index where these characters are not found front the beginning and end. The type of whitespaces are explained as above.
Method 3: C++ Boost Library
#include <bits/stdc++.h> #include <boost/algorithm/string.hpp> using namespace std; string trimfnc(string str) { boost::trim_right(str); } int main() { string str; getline(cin, str); str = trimfnc(str); cout<<str; }
In this case you will need the boost library available for your compiler, it can be found here: https://www.boost.org/
All the above methods work in linear time and all of them can be made to work in no extra space (in-place) if we pass the reference of the string instead of a copy of it. For understanding purpose it is passed by reference in examples.
The second method is widely used because of short code, no extra library requirement and clean solution.