-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.cpp
51 lines (42 loc) · 1.12 KB
/
str.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "str_util.h"
#include <sstream>
#include <algorithm>
#include <functional>
namespace str
{
std::vector<std::string> split( std::string const& sentence, char delimeter)
{
using namespace std;
stringstream ss(sentence);
vector<string> words;
string word;
while( getline( ss, word, delimeter ) )
{
words.push_back(word);
}
return words;
}
void replace( std::string& sentence, std::string const& old_phrase, std::string const& new_phrase)
{
size_t pos = sentence.find( old_phrase, 0);
while( pos != std::string::npos )
{
sentence.replace( pos++, old_phrase.size(), new_phrase );
pos = sentence.find( old_phrase, pos );
}
}
static inline std::string& ltrim( std::string& str)
{
str.erase(str.begin(), std::find_if(str.begin(), str.end(),
std::not1(std::ptr_fun<int, int>(std::isspace) ) ) );
return str;
}
static inline std::string& rtrim(std::string& str) {
str.erase(std::find_if(str.rbegin(), str.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace) ) ).base(), str.end());
return str;
}
std::string& trim( std::string& str) {
return ltrim(rtrim(str));
}
}