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 :: c++ - 
Cpp :: c++ print array of arrays with pointer 
Cpp :: how to run cpp using gcc vscode 
Cpp :: get function in cpp. 
Cpp :: c++ if else example 
Cpp :: c++ sorting and keeping track of indexes 
Cpp :: delete c++ 
Cpp :: C++ vector structure 
Cpp :: vector iterator in c++ 
Cpp :: c++ function pointer as variable 
Cpp :: definition of singly linkedlist 
Cpp :: 1. Two Sum 
Cpp :: Array Rotate in c++ 
Cpp :: raspberry pi mount external hard drive 
Cpp :: c++ char 
Cpp :: data type c++ 
Cpp :: std::enable_shared_from_this include 
Cpp :: array bubble sort c++ static 
Cpp :: c++ convert int to string with a fixed number of digits 
Cpp :: vector keyword in c++ 
Cpp :: C++ Initializing a thread with a class/object 
Cpp :: point in polygon 
Cpp :: cpp pass function with input to thread 
Cpp :: print float up to 3 decimal places in c++ 
Cpp :: c++ program for inflation rate of two numbers 
Cpp :: how to use string in if else statement c++ 
Cpp :: destiny child 
Cpp :: what is blob in computer vision 
Cpp :: c++ correct upto 3 decimal places 
Cpp :: how to get max grade c++ 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =