How to trim a string in C++?

In this article, we will discuss different techniques to trim a string in C++.

Table Of Contents

Introduction

When we say we want to trim a string, it means to remove the whitespaces from the start and end of the string.

When we trim a string, whiteSpaces in between string will not be removed.

Example,

Advertisements

” this pointer ” will become “this pointer” after trim operation.

Method 1: C++ standard functions

To trim from the start of the string by removing leading whitespace
* string::find_first_not_of(whitespace)– finding the index just after leading whitespaces from start of string.
* string::erase(0,index) – erases all characters from start upto first non-space index of white spaces.

To trim from end of the string by removing trailing whitespace
* string::find_last_not_of(whitespace) – finding the index just before trailing whitespaces from end of string.
* string::erase(index) – erases all characters after index position.

// C++ program to trim a string (remove white spaces in a given string)
#include <iostream>
#include <string>
using namespace std;

string whiteSpaces = " \n\r\t\f\v"; // all types of possible spaces to remove

string trimString(string str) {
    const string whiteSpaces = " \t\n\r\f\v";

    // Remove leading whitespace
    size_t first_non_space = str.find_first_not_of(whiteSpaces);
    str.erase(0, first_non_space);

    // Remove trailing whitespace
    size_t last_non_space = str.find_last_not_of(whiteSpaces);
    str.erase(last_non_space + 1);

    return str;
}

int main()
{
    string str = "\n\t  This Pointer  \r\n";
    cout<< "Before: " << str;
    cout<< "After : " << trimString(str);

    return 0;
}

Output:

Before: 
      This Pointer  

After : This Pointer

We have trimmed the string by removing the whitespaces from start and end of the string.

Method 2: Using Boost Library function ‘trim()’

The Boost library provides trim_left() , trim_right() and trim() functions fit for our purpose.

  • boost::algorithm::trim_right : trim right side of the string.
  • boost::algorithm::trim_left : trim left side of the string
  • boost::algorithm::trim : trim both side of the string.

In our example, we will use ‘trim()’ function.

// C++ program to trim a string (remove white spaces in a given string)
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

int main()
{
    std::string str = "\n\t  This Pointer  \r\n";
    std::cout<< "Before: " << str;
    boost::algorithm::trim(str);
    std::cout<< "After : " << str;

    return 0;
}

Output:

Before: 
      This Pointer  

After : This Pointer

We have trimmed the string by removing the whitespaces from start and end of the string.

Method 3 : Using std::regex_replace function (C++11)

We will use the power of regular expression to trim the string from start and end.
The header file ‘regex’ will provide std::regex_replace() function which will use the regular expression to replace whitespaces from start and end of the string using regular expression.

// C++ program to trim a string (remove white spaces in a given string)
#include <iostream>
#include <string>
#include <regex>

// Trim whitespaces from start and end of string
std::string trim(const std::string &s) {
    std::regex pattern("^\\s+|\\s+$");
	return std::regex_replace(s, pattern, "");
}
 
int main()
{
	std::string str = "\n\t  This Pointer  \r\n";
	std::cout<< "Before: " << str << std::endl;
	std::cout<< "After : " << trim(str) << std::endl;
	
    return 0;
} 

Output:

Before: 
      This Pointer  

After : This Pointer

We have trimmed the string by removing the whitespaces from start and end of the string.

Method 4: Custom Function

We will write our custom function ‘trim()’ which will perform following:
* find the ‘start’ index of non-whitespace character from the start.
* find the ‘end’ index of non-whitespace character from the end.
* return a string between the ‘start’ and ‘end’

// C++ program to trim a string (remove white spaces in a given string)
#include <iostream>
#include <string>
#include <algorithm>

std::string trim(const std::string &s)
{
    auto start = s.begin();
    auto end = s.end();

    while (std::isspace(*start) && start != end) {
        start++;
    }

    do {
        end--;
    } while (std::isspace(*end) && start > end);

    return std::string(start, end + 1);
}

int main()
{
    std::string str = "\n\t  This Pointer  \r\n";
    std::cout<< "Before: " << str << std::endl;
    std::cout<< "After : " << trim(str);

    return 0;
}

Output:

Before: 
     This Pointer  


After : This Pointer 

We have trimmed the string by removing the whitespaces from start and end of the string.

Summary

We learned different ways to trim the string in C++. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top