1#include <algorithm>
2
3int main()
4{
5 std::string str = "H e l l o";
6 str.erase(remove(str.begin(), str.end(), ' '), str.end());
7 std::cout << str; // Output Hello
8
9 return 0;
10}
1static std::string removeSpaces(std::string str)
2{
3 str.erase(remove(str.begin(), str.end(), ' '), str.end());
4 return str;
5}
1string removeSpaces(string str)
2{
3 stringstream s(str);
4 string temp;
5 str = "";
6 while (getline(s, temp, ' ')) {
7 str = str + temp;
8 }
9 return str;
10}
11//Input: Ha Noi Viet Nam
12//Output: HaNoiVietNam
1#include <algorithm>
2#include <cctype>
3#include <locale>
4
5// trim from start (in place)
6static inline void ltrim(std::string &s) {
7 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
8 return !std::isspace(ch);
9 }));
10}
11
12// trim from end (in place)
13static inline void rtrim(std::string &s) {
14 s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
15 return !std::isspace(ch);
16 }).base(), s.end());
17}
18
19// trim from both ends (in place)
20static inline void trim(std::string &s) {
21 ltrim(s);
22 rtrim(s);
23}
24
25// trim from start (copying)
26static inline std::string ltrim_copy(std::string s) {
27 ltrim(s);
28 return s;
29}
30
31// trim from end (copying)
32static inline std::string rtrim_copy(std::string s) {
33 rtrim(s);
34 return s;
35}
36
37// trim from both ends (copying)
38static inline std::string trim_copy(std::string s) {
39 trim(s);
40 return s;
41}
42