Trim String in C++

Trim String in C++

When we take an input from an user, strings can have unwanted whitespaces in the start or end of the string. We can trim the strings to get rid of the extra whitespaces in the start and in the end, but not in the middle of the String.

For example, if a user inputs some whitespace in the starting or ending of his email id, then it should be sent without any leading or trailing whitespaces to the servers for the accurate information.

C++ does not have direct support for string trimming, but we can make use of boost library, a combination of library functions and custom functions to trim the string in C++.

In this article, we will see different ways to trim String in C++.

Using Boost string algorithms to trim string in C++

We can utilize the trim_right and trim_left functions defined in the boost string algorithms. These functions can be used to remove the right and left whitespaces from the string. We can also use the trim_right_copy or trim_left_copy functions that will return the trimmed copy of the string.

This library also provides the function trim_copy_if that will only trim the string if the given condition is satisfied.

Code

Input

“ Java2 Blog “
“ C++ Programming “
“ Java Programming “

Output

Original Input String1: Java2 Blog
String after trim(): Java2 Blog
Original Input String2: C++ Programming
String after trim_left(): C++ Programming
String after trim_right(): C++ Programming
Original string: Java Programming
Output String after left trim: Java Programming
Output String after right trim: Java Programming
Mobile Number without leading zeros: 987654321

Using find_first_not_of() and find_last_not_of()` to trim strings in C++

In this method, we will use find_first_not_of() and find_last_not_of() to create left trim and right trim functions. These two functions can be used to search the string for the first and last character that does not match with the characters specified in the argument of the functions.

If position is also specified in the function, then it will only include the characters present at that position or after that position (ignores all the characters present before the position).

Code

Input

“ Java2Blog “
“ C++ Programming “

Output

Original String 1: Java2Blog
After Trim: Java2Blog
Original String 2: C++ Programming
After Left Trim: C++ Programming
After Right Trim: C++ Programming

Using find_if() to trim the string in C++

We can utilize the find_if() function for making customized functions for left trim and right trim of unwanted characters.

Syntax of find_if():

Parameters:
start, end: These are input iterators to the starting and ending positions in the sequence. The range [start, end) contains all the elements between start and end. It does not include the element pointed by end and includes the element pointed by start.

pred: It is a unary function that can accept an element in the range as arguments and it returns a bool value.

Return Value: It returns an iterator to the first element for which pred returns true. If pred is not true for any element, then it will return an iterator to the last element.

Code

Output

Java2Blog
C++ Programming
C++ Programming

Using stringstream to trim the strings in C++

We can use the stringstream method if there are no internal whitespaces. It is a less efficient method because the string gets copied in the stream. If there are any internal whitespaces then it will return the characters before that whitespace.

Note: It is not possible to make left trim and right trim functions in this method. We can directly trim the string from both sides.

Code

Output

String After Trim: JAVA2BLOG
String After Trim: C++

Using a customized function to trim the strings in C++

Here, we count the number of leading or trailing whitespaces. We get the position where the valid character starts and where it ends, then we return only that part of the string.

Code

Output

String After Trim:JAVA2 BLOG

Conclusion

We discussed efficient methods to trim strings in C++, though there is no direct support for that. Any method can be used and every method has a different level of complexity. Trimming string can be easy if the above methods are followed.

That’s all about how to trim String in C++.
Happy Learning!!

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *