-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.cpp
More file actions
59 lines (50 loc) · 1.38 KB
/
StringUtils.cpp
File metadata and controls
59 lines (50 loc) · 1.38 KB
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
52
53
54
55
56
57
58
59
/*
* 字符串工具接口实现
*/
#include"StringUtils.h"
string doubleToString(double num) {
char* buf;//double数据转换为无符号整数字符串 如12.34 -> 1234
int dec, sign;//dec->小数点前有几位数字 sign->0为正数,1为负数
buf = _fcvt(num, 2, &dec, &sign);
string str;
if (sign) str += "-";
int i;
for (i = 0; i < dec; i++) {
str += buf[i];
}
if (dec == 0)
str += "0";
if (strcmp(buf, "00") != 0) {
str += ".";
for (; i < strlen(buf); i++) {
str += buf[i];
}
}
return str;
}
std::string trim(const std::string& str) {
const std::string& whitespace = " \t";
const auto strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content
const auto strEnd = str.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
std::string reduce(const std::string& str) {
const std::string& fill = " ";
const std::string& whitespace = " \t";
// trim first
auto result = trim(str);
// replace sub ranges
auto beginSpace = result.find_first_of(whitespace);
while (beginSpace != std::string::npos)
{
const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
const auto range = endSpace - beginSpace;
result.replace(beginSpace, range, fill);
const auto newStart = beginSpace + fill.length();
beginSpace = result.find_first_of(whitespace, newStart);
}
return result;
}