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

  • Round to 2 decimal places in C++
    25 January

    Round to 2 decimal places in C++

    Table of ContentsRepresenting Decimal Numbers in C++Ways to round to 2 decimal places in C++Using the ceil() function to round to 2 decimal places in C++Using the floor() function to round to 2 decimal places in C++Using the int typecast to round to 2 decimal places in C++Using the std::setprecision() function to round to 2 […]

  • Fill array with random numbers c++
    19 January

    Fill Array With Random Numbers in C++

    Table of ContentsThe rand() Function in C++How to Fill the Array With Random Numbers in C++?How to Fill the Array With Random Numbers in a Range in C++?Conclusion IN this post, we will see how to fill array with random number in C++. Arrays are the most commonly used data structures in the programming domain. […]

  • Convert ASCII to char in C++
    15 January

    Convert ASCII to Char in C++

    Table of ContentsHow to Convert ASCII values to Char in C++?Convert ASCII to Char in C++ Using Char() FunctionConvert ASCII to Char in C++ Using C-Style CastConvert ASCII to Char in C++ Using the Assignment OperatorConvert ASCII to Character in C++ Using sprintf() FunctionConclusion The characters are represented using integers in most of the encoding […]

  • Check if input is Integer in C++
    13 January

    Check If Input Is Integer In C++

    Table of ContentsHow to Check Whether the Input Is an Integer in C++?Check Whether the Input Is an Integer Using the isdigit() Function in C++Check Whether the Input Is an Integer Using the Ascii Values in C++Conclusion C++ is a powerful and popular programming language and we use it widely in coding applications. It provides […]

  • Convert String to lowercase in C++
    11 January

    Convert String to lowercase in C++

    Table of ContentsWays to Convert a String to Lowercase in C++Using the tolower() and transform() functionUsing the for loopUsing the boost library In this post, we will see how to convert String to lowercase in C++. We can assume strings to be an array of characters. Each character has a specific ASCII value, and characters […]

  • Split String by comma in C++
    10 November

    Split String by comma in C++

    Table of ContentsUsing the for loopUsing the getline() functionUsing the strtok() functionUsing the std::string::erase() and std::string::find() function In this article, we will split a string by comma in C++. There are several methods available to achieve this. For this, we can create a function and use some inbuilt functions. Using the for loop In this […]

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.