How to Trim a String in C++

Jinku Hu Feb 02, 2024
How to Trim a String in C++

This article will explain how to trim a string in C++.

Use erase(), find_first_not_of() and find_last_not_of() Methods to Implement String Trimming Functions

Since the standard C++ library does not contain the functions for string trimming, you will need to implement them yourself or use external libraries like Boost (see string algorithms).

In the following examples, we demonstrate how to construct custom functions using 2 built-in std::string methods. Firstly, we implement the leftTrim function, which trims characters passed as an argument from the string’s left side. We arbitrarily specified characters ., ,, / and whitespace to be trimmed.

leftTrim function calls the find_first_not_of method to find the first character not equal to the char-s in the given argument and returns a position to the found one. Then the erase method removes the range of characters from the beginning until the found position.

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

string& leftTrim(string& str, string& chars) {
  str.erase(0, str.find_first_not_of(chars));
  return str;
}

int main() {
  string chars_to_trim = ".,/ ";
  string text = ",.,  C++ Standard";

  cout << text << endl;
  leftTrim(text, chars_to_trim);
  cout << text << endl;

  return EXIT_SUCCESS;
}

Output:

,.,  C++ Standard
C++ Standard

Alternatively, we can revert the trimLeft function to trim the given characters from the string’s right side. In this case, we utilize the find_last_not_of method, which searches for the last equal to none of the characters passed as an argument. Correspondingly, the erase method is called with the found position + 1 parameter.

Notice that both of these functions operate on the string in place and not returning a copy of the trimmed version.

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

string& rightTrim(string& str, string& chars) {
  str.erase(str.find_last_not_of(chars) + 1);
  return str;
}

int main() {
  string chars_to_trim = ".,/ ";
  string text = "C++ Standard /././";

  cout << text << endl;
  rightTrim(text, chars_to_trim);
  cout << text << endl;

  return EXIT_SUCCESS;
}

Output:

C++ Standard /././
C++ Standard

Finally, we can combine the previous functions to implement the trimString function, which removes both sides’ characters. The function has the same parameters as the left/right versions. trimString calls leftTrim by passing it the result of the rigthTrim function as an argument. You can swap places of these function calls without altering the correctness of the program.

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

string& leftTrim(string& str, string& chars) {
  str.erase(0, str.find_first_not_of(chars));
  return str;
}

string& rightTrim(string& str, string& chars) {
  str.erase(str.find_last_not_of(chars) + 1);
  return str;
}

string& trimString(string& str, string& chars) {
  return leftTrim(rightTrim(str, chars), chars);
}

int main() {
  string chars_to_trim = ".,/ ";
  string text = ",,, ..    C++ Standard  ...";

  cout << text << endl;
  trimString(text, chars_to_trim);
  cout << text << endl;

  return EXIT_SUCCESS;
}
,,, ..    C++ Standard  ...
C++ Standard
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ String