-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrim.cpp
More file actions
36 lines (31 loc) · 792 Bytes
/
trim.cpp
File metadata and controls
36 lines (31 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*****************************************************
* This code was written by jotik and is not my work
* Source:
* https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
*****************************************************/
#include <algorithm>
#include <cctype>
#include <locale>
#include <string>
#include "trim.h"
using namespace std;
// trim from start (in place)
void ltrim(string &s)
{
s.erase(s.begin(), find_if(s.begin(), s.end(), [](int ch) {
return !isspace(ch);
}));
}
// trim from end (in place)
void rtrim(string &s)
{
s.erase(find_if(s.rbegin(), s.rend(), [](int ch) {
return !isspace(ch);
}).base(), s.end());
}
// trim from both ends (in place)
void trim(string &s)
{
ltrim(s);
rtrim(s);
}