Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ remove whitespace from string

#include <algorithm>

int main()
{
    std::string str = "H e l l o";
    str.erase(remove(str.begin(), str.end(), ' '), str.end());
    std::cout << str; // Output Hello
    
    return 0;
}
Comment

c++ remove space from string

static std::string removeSpaces(std::string str)
{
	str.erase(remove(str.begin(), str.end(), ' '), str.end());
	return str;
}
Comment

strip space from string cpp

#include<iostream>
#include<algorithm>
using namespace std;
main() {
   string my_str = "This is C++ Programming Language";
   cout << "String with Spaces :" << my_str << endl;
   remove(my_str.begin(), my_str.end(), ' ');
   cout << "String without Spaces :" << my_str;
}
Comment

remove space in string c++

string removeSpaces(string str)
{
    stringstream s(str);
    string temp;
    str = "";
    while (getline(s, temp, ' ')) {
        str = str + temp;
    }
    return str;
}
//Input: Ha Noi Viet Nam
//Output: HaNoiVietNam
Comment

strip whitespace c++

#include <algorithm> 
#include <cctype>
#include <locale>

// trim from start (in place)
static inline void ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
        return !std::isspace(ch);
    }));
}

// trim from end (in place)
static inline void rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
        return !std::isspace(ch);
    }).base(), s.end());
}

// trim from both ends (in place)
static inline void trim(std::string &s) {
    ltrim(s);
    rtrim(s);
}

// trim from start (copying)
static inline std::string ltrim_copy(std::string s) {
    ltrim(s);
    return s;
}

// trim from end (copying)
static inline std::string rtrim_copy(std::string s) {
    rtrim(s);
    return s;
}

// trim from both ends (copying)
static inline std::string trim_copy(std::string s) {
    trim(s);
    return s;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to add colored text in c++ 
Cpp :: c++ find element in vector 
Cpp :: c++ case 
Cpp :: c++ for in 
Cpp :: multiline comment in c++ 
Cpp :: how to write something in power of a number in c++ 
Cpp :: access part of string in c++ 
Cpp :: c++ constructors 
Cpp :: how to make calculaor in c++ 
Cpp :: c++ function as param 
Cpp :: c++ length of char array 
Cpp :: how to make a list in c++ 
Cpp :: fstring from float c++ ue4 
Cpp :: find in string c++ 
Cpp :: sieve cpp 
Cpp :: string to decimal c++ strtol 
Cpp :: ray sphere intersection equation 
Cpp :: count number of set bits C++ 
Cpp :: casting c++ 
Cpp :: reading file c++ 
Cpp :: stoi cpp 
Cpp :: how to dynamically allocate an array c++ 
Cpp :: c++ code for bubble sort 
Cpp :: find prime number c++ 
Cpp :: what is a template in c++ 
Cpp :: how do you wait in C++ 
Cpp :: pragma cpp 
Cpp :: card validator c++ 
Cpp :: iterate over map c++ 
Cpp :: struct c++ 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =