-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringhelper.h
101 lines (87 loc) · 2.7 KB
/
stringhelper.h
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
//----------------------------------------------------------------------------------------
// Copyright © 2007 - 2018 Tangible Software Solutions Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
//
// This class is used to replace some string methods, including
// conversions to or from strings.
//----------------------------------------------------------------------------------------
#include <string>
#include <sstream>
#include <vector>
class StringHelper
{
public:
static std::wstring toLower(std::wstring source)
{
std::transform(source.begin(), source.end(), source.begin(), std::tolower);
return source;
}
static std::wstring toUpper(std::wstring source)
{
std::transform(source.begin(), source.end(), source.begin(), std::toupper);
return source;
}
static std::wstring trimStart(std::wstring source, const std::wstring &trimChars = L" \t\n\r\v\f")
{
return source.erase(0, source.find_first_not_of(trimChars));
}
static std::wstring trimEnd(std::wstring source, const std::wstring &trimChars = L" \t\n\r\v\f")
{
return source.erase(source.find_last_not_of(trimChars) + 1);
}
static std::wstring trim(std::wstring source, const std::wstring &trimChars = L" \t\n\r\v\f")
{
return trimStart(trimEnd(source, trimChars), trimChars);
}
static std::wstring replace(std::wstring source, const std::wstring &find, const std::wstring &replace)
{
size_t pos = 0;
while ((pos = source.find(find, pos)) != std::string::npos)
{
source.replace(pos, find.length(), replace);
pos += replace.length();
}
return source;
}
static bool startsWith(const std::wstring &source, const std::wstring &value)
{
if (source.length() < value.length())
return false;
else
return source.compare(0, value.length(), value) == 0;
}
static bool endsWith(const std::wstring &source, const std::wstring &value)
{
if (source.length() < value.length())
return false;
else
return source.compare(source.length() - value.length(), value.length(), value) == 0;
}
static std::vector<std::wstring> split(const std::wstring &source, wchar_t delimiter)
{
std::vector<std::wstring> output;
std::wistringstream ss(source);
std::wstring nextItem;
while (std::getline(ss, nextItem, delimiter))
{
output.push_back(nextItem);
}
return output;
}
template<typename T>
static std::wstring toString(const T &subject)
{
std::wostringstream ss;
ss << subject;
return ss.str();
}
template<typename T>
static T fromString(const std::wstring &subject)
{
std::wistringstream ss(subject);
T target;
ss >> target;
return target;
}
};