-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstr.cpp
More file actions
33 lines (24 loc) · 795 Bytes
/
Copy pathstr.cpp
File metadata and controls
33 lines (24 loc) · 795 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
#include <iostream>
#include "str.h"
std::string str_trim(std::basic_string<char> str) {
str.erase(str.find_last_not_of(' ') + 1);
str.erase(0, str.find_first_not_of(' '));
return str;
}
bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
std::string replaceAll(std::string str, const std::string& from, const std::string& to) {
if(from.empty())
return str;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return str;
}