-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtil.cpp
More file actions
82 lines (62 loc) · 1.68 KB
/
Copy pathStringUtil.cpp
File metadata and controls
82 lines (62 loc) · 1.68 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "StringUtil.h"
#include <sstream>
namespace dxco {
std::string StringUtil::toString(int data) {
std::ostringstream ss;
ss << data;
return ss.str();
}
std::string StringUtil::toString(float data) {
std::ostringstream ss;
ss << data;
return ss.str();
}
std::string StringUtil::intToKString(int number) {
if (number < 1000) { // full number
return StringUtil::toString(number);
}
if (number < 10000) { // 1 decimal place
return StringUtil::toString((float)((int)((number / 1000.0) * 10) / 10.0)) + "k";
}
return StringUtil::toString(number / 1000) + "k"; // no decimal place
}
int StringUtil::toInt(std::string data) {
int value;
std::istringstream ss(data);
ss >> value;
return value;
}
std::vector<std::string> StringUtil::split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::string StringUtil::padLeft(std::string &str, const int stringSize, const char paddingChar)
{
if(stringSize > str.size()) {
str.insert(0, stringSize - str.size(), paddingChar);
}
return str;
}
std::string StringUtil::padLeft(int number, const int stringSize)
{
std::string numberString = toString(number);
return padLeft(numberString, stringSize, '0');
}
std::string StringUtil::sFormat(std::string s, std::string value) {
char buff[200];
sprintf(buff, s.c_str(), value.c_str());
std::string buffAsStdStr = buff;
return buff;
}
std::string StringUtil::iFormat(std::string s, int value) {
char buff[200];
sprintf(buff, s.c_str(), value);
std::string buffAsStdStr = buff;
return buff;
}
} /* namespace dxco */