A Deeper Look at the std::string Class

Removing Whitespace from a String

What's the most efficient way to remove all whitespace from a std::string?

Abstract art representing computer programming

Removing all whitespace from a std::string is a common task in string processing, especially when cleaning up user input or formatting data. Here's an efficient way to accomplish this using the standard library:

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

std::string remove_whitespace(std::string str) {
  str.erase(std::remove_if(
    str.begin(), str.end(),
    [](unsigned char c) {
      return std::isspace(c);
    }),
    str.end()
  );
  return str;
}

int main() {
  std::string text{"  Hello,   World!  \t\n"};

  std::cout << "Original: '" << text << "'\n";
  std::cout << "Without whitespace: '"
            << remove_whitespace(text) << "'\n";
}
Original: '  Hello,   World!
'
Without whitespace: 'Hello,World!'

Let's break down the remove_whitespace() function:

  1. We use std::remove_if() to move all non-whitespace characters to the front of the string.
  2. The lambda function [](unsigned char c) { return std::isspace(c); } checks if a character is whitespace.
  3. std::isspace() from <cctype> checks for any whitespace character (space, tab, newline, etc.).
  4. str.erase() then removes all the whitespace characters that were moved to the end of the string.

This approach is known as the erase-remove idiom and is very efficient because it only traverses the string once and minimizes memory allocations.

If you want to modify the string in-place instead of returning a new string, you can do:

void remove_whitespace(std::string& str) {
  str.erase(std::remove_if(
    str.begin(), str.end(),
    [](unsigned char c) {
      return std::isspace(c);
    }),
    str.end()
  );
}

For even more efficiency, especially with longer strings, you can use std::string::reserve() to avoid reallocations:

std::string remove_whitespace(
  const std::string& str
) {
  std::string result;

  // Pre-allocate space
  result.reserve(str.length());

  for (char c : str) {
    if (!std::isspace(
      static_cast<unsigned char>(c)
    )) {
      result += c;
    }
  }

  return result;
}

This method might be slightly faster for very long strings as it avoids moving characters around, but it does create a new string. Choose the method that best fits your specific use case and performance needs.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved