-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringFunctions.h
180 lines (136 loc) · 2.98 KB
/
StringFunctions.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***
String Functions: Commonly used string functions
***/
#ifndef STRING_FUNCTIONS_H
#define STRING_FUNCTIONS_H
#include <string>
class StringFunctions
{
public:
// Will return the nth string value of a delimited string
static std::string getPram( std::string& data, int position, char delimiter )
{
if( position < 1 )
{
position = 1;
}
int pram = 1;
int start = -1;
int end = data.size()-1;
int size = 0;
while( pram < position )
{
start = data.find( delimiter, start+1 );
if( start == std::string::npos )
return "";
pram++;
}
start++;
if( data[start] == delimiter )
return "";
end = data.find( delimiter, start+1 );
if( end == std::string::npos )
{
size = data.size() - start;
}
else
{
size = end - start;
}
return data.substr( start, size ).c_str() ;
}
// Will return the number of prams in a delimited string
static int getNumOfPrams( std::string& data, char delimiter )
{
int pram = 1;
int start = -1;
while( true )
{
start = data.find( delimiter, start+1 );
if( start == std::string::npos )
return pram;
pram++;
}
}
// Will remove all occurrences of specified string
static void spliceOut( std::string& data, std::string unwanted, std::string replace = "" )
{
int occPos = data.find( unwanted );
while( occPos != std::string::npos )
{
data.replace( occPos, unwanted.size(), replace );
occPos = data.find( unwanted );
}
}
// Retuns true if the string starts with the find value
// -Returns true for comparison with ""
static bool beginsWith( std::string data, std::string find, bool caseSensitive = false )
{
int pos = 0;
int length = find.size();
if( !caseSensitive )
{
toLower( data );
toLower( find );
}
while( pos < length )
{
if( data[pos] != find[pos] )
return false;
pos++;
}
return true;
}
// Will convert all uppercase letters to lower case
static void toLower( std::string& data )
{
int pos = 0;
int length = data.size();
while( pos < length )
{
if( data[pos] > 64 && data[pos] < 91 )
data[pos] += 32;
pos++;
}
}
// Will convert all lowercase letters to upper case
static void ToUpper( std::string& data )
{
int pos = 0;
int length = data.size();
while( pos < length )
{
if( data[pos] > 96 && data[pos] < 123 )
data[pos] -= 32;
pos++;
}
}
// Will remove the front n members of a string
static void removeFront( std::string& data, int num )
{
data.erase( 0, num );
}
// Will erase from either the front or the back until the find char is found
static void EraseUntilCharFound( std::string& data, bool fromFront, char find, bool inclusive = false )
{
if( data.size() <= 0 )
return;
int pos = 0;
if( !fromFront )
pos = data.size()-1;
while( data.size() != 0 )
{
if( data[pos] == find )
{
if( !inclusive )
return;
data.erase(pos,1);
return;
}
data.erase(pos,1);
if( !fromFront )
pos--;
}
}
};
#endif // !STRING_HFUNCTIONS_H