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

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 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 :: type casting in cpp 
Cpp :: c++ array on heap 
Cpp :: c++ map key exists 
Cpp :: int to string Using to_string method 
Cpp :: short hand if else in c++ 
Cpp :: how to create a structure c++ 
Cpp :: longest increasing subsequence nlogn c++ 
Cpp :: what is push() c++ 
Cpp :: sum function in c++ 
Cpp :: c++ inline if 
Cpp :: call by value in c++ 
Cpp :: a function to create double quotes for alphabet in c++ 
Cpp :: qregexpvalidator qlineedit email address 
C :: malloc is undefined 
C :: terminal count files in directory 
C :: Sorting number excluding elements in highest to lowest 
C :: Which of the following are Cetaceans? 
C :: c code to python code converter online 
C :: how to get user input in c 
C :: Calculator_C 
C :: how to read space separated words in c 
C :: unity set transform position code 
C :: divide and conquer program in c 
C :: why do we need return 0 in c? 
C :: install tweaks ubuntu 
C :: print short in c 
C :: arduino wifi client 
C :: add_to_cart how to call it woocommerce 
C :: bootstrap 4 forms 
C :: fibonacci series in c 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =