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!!

Related Posts

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

  • Enum to String in C++
    19 July

    Convert enum to string in C++

    Table of ContentsUsing stringify macro method to convert enum to String in C++Using const char* Array to convert enum to String in C++Using a custom-defined function to convert enum to String in C++Conclusion There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

  • String contains C++
    07 June

    Check if string contains substring in C++

    Table of ContentsUsing find() function to check if string contains substring in C++.Using strstr() method to check if string contains substring in C++Using boost library to check if string contains substring in C++Using custom functions to check if string contains substring in C++Conclusion There are different ways to check if string contains substring in C++. […]

  • Convert float to String in C++
    24 May

    Convert float to String in C++

    Learn about how to convert float to String in C++ in different wayds.

  • Convert int to char array in C++
    18 May

    Convert int to Char Array in C++

    Table of ContentsUsing to_string and c_str methods to convert int to Char Array in C++Using stringstream to convert int to Char Array in C++Using to_chars method to convert int to Char Array in C++Using sprintf function to convert int to Char Array in C++Using Boost:: lexical_cast method to convert int to Char Array in C++Conclusion […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.