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

remove whitespace in cpp

string removeWhiteSpaces(string line) {
	string s = "";
	int space = 0;
	while (true) {
		if (line[space] == ' ')
			space++;
		else break;
	}
	for (int x = space; x < line.length(); x++)
		s += line[x];

	space = s.length() - 1;
	while (true) {
		if (s[space] == ' ') {
			space--;
		}
		else 
			break;
	}
	for (int x = 0; x <= space; x++)
		s[x] = s[x];
	s[space + 1] = '';
	return s;
}

input  = "           String            "
ouput = "String"
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 :: initialize all elements of vector to 0 c++ 
Cpp :: c++ Modulo 10^9+7 (1000000007) 
Cpp :: how to writt array in c++ 
Cpp :: delete specific vector element c++ 
Cpp :: casting pointer (int to char*) in c++ 
Cpp :: n queens c++ 
Cpp :: oncomponentbeginoverlap ue4 c++ 
Cpp :: cout.flush() in c++ 
Cpp :: c++ split string by space into vector 
Cpp :: std string to const char * c++ 
Cpp :: cout was not declared in this scope 
Cpp :: format c++ discord 
Cpp :: c++ cli convert string to string^ 
Cpp :: initialize 2d array c++ memset 
Cpp :: time delay in c++ 
Cpp :: c++ unordered_map check if key exists 
Cpp :: online cpp to exe converter 
Cpp :: run c++ file putty 
Cpp :: c++ print vector without loop 
Cpp :: maximum int c++ 
Cpp :: c++ for in 
Cpp :: c++ struct with default values 
Cpp :: c++ return multiple values 
Cpp :: print all elements of vector c++ 
Cpp :: max heap in c++ 
Cpp :: check if character is uppercase c++ 
Cpp :: how to convert string into lowercase in cpp 
Cpp :: c++ read each char of string 
Cpp :: reading file c++ 
Cpp :: how to debug c++ code in vs studio code 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =