#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;
}
static std::string removeSpaces(std::string str)
{
str.erase(remove(str.begin(), str.end(), ' '), str.end());
return str;
}
#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;
}
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
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"
#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;
}
Code Example |
---|
Cpp :: how can I replace a pattern from string in c++ |
Cpp :: cannot find -lsqlite3 C++ compiler error |
Cpp :: qstring insert character |
Cpp :: c++ find key in hashmap |
Cpp :: std cout c++ |
Cpp :: read string from binary file in c++ |
Cpp :: c++ std::fmin |
Cpp :: c++ main environment variables |
Cpp :: add partition mysql |
Cpp :: binary exponentiation modulo m |
Cpp :: math in section title latex |
Cpp :: convert string to char c++ |
Cpp :: convert decimal to binary c++ |
Cpp :: extends c++ |
Cpp :: structure and function c++ |
Cpp :: how to find size of int array in c++ |
Cpp :: c++ lock |
Cpp :: c++ map loop through key value |
Cpp :: copy 2 dimensional array c++ |
Cpp :: copy array c++ |
Cpp :: chrono library c++ |
Cpp :: set precision with fixed c++ |
Cpp :: bubble sort in c+ |
Cpp :: max of a vector c++ |
Cpp :: doubly linked list c++ code |
Cpp :: segmented sieve cpp |
Cpp :: c++ first letter of string |
Cpp :: string to int c++ |
Cpp :: how to print in cpp |
Cpp :: priority queue c++ |