-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsString.h
520 lines (366 loc) · 9.49 KB
/
clsString.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//ProgrammingAdvices.com
//Mohammed Abu-Hadhoud
#pragma once
#include <iostream>
#include <vector>
using namespace std;
class clsString
{
private:
string _Value;
public:
clsString()
{
_Value = "";
}
clsString(string Value)
{
_Value = Value;
}
void SetValue(string Value) {
_Value = Value;
}
string GetValue() {
return _Value;
}
__declspec(property(get = GetValue, put = SetValue)) string Value;
static short Length(string S1)
{
return S1.length();
};
short Length()
{
return _Value.length();
};
static short CountWords(string S1)
{
string delim = " "; // delimiter
short Counter = 0;
short pos = 0;
string sWord; // define a string variable
// use find() function to get the position of the delimiters
while ((pos = S1.find(delim)) != std::string::npos)
{
sWord = S1.substr(0, pos); // store the word
if (sWord != "")
{
Counter++;
}
//erase() until positon and move to next word.
S1.erase(0, pos + delim.length());
}
if (S1 != "")
{
Counter++; // it counts the last word of the string.
}
return Counter;
}
short CountWords()
{
return CountWords(_Value);
};
static string UpperFirstLetterOfEachWord(string S1)
{
bool isFirstLetter = true;
for (short i = 0; i < S1.length(); i++)
{
if (S1[i] != ' ' && isFirstLetter)
{
S1[i] = toupper(S1[i]);
}
isFirstLetter = (S1[i] == ' ' ? true : false);
}
return S1;
}
void UpperFirstLetterOfEachWord()
{
// no need to return value , this function will directly update the object value
_Value = UpperFirstLetterOfEachWord(_Value);
}
static string LowerFirstLetterOfEachWord(string S1)
{
bool isFirstLetter = true;
for (short i = 0; i < S1.length(); i++)
{
if (S1[i] != ' ' && isFirstLetter)
{
S1[i] = tolower(S1[i]);
}
isFirstLetter = (S1[i] == ' ' ? true : false);
}
return S1;
}
void LowerFirstLetterOfEachWord()
{
// no need to return value , this function will directly update the object value
_Value = LowerFirstLetterOfEachWord(_Value);
}
static string UpperAllString(string S1)
{
for (short i = 0; i < S1.length(); i++)
{
S1[i] = toupper(S1[i]);
}
return S1;
}
void UpperAllString()
{
_Value = UpperAllString(_Value);
}
static string LowerAllString(string S1)
{
for (short i = 0; i < S1.length(); i++)
{
S1[i] = tolower(S1[i]);
}
return S1;
}
void LowerAllString()
{
_Value = LowerAllString(_Value);
}
static char InvertLetterCase(char char1)
{
return isupper(char1) ? tolower(char1) : toupper(char1);
}
static string InvertAllLettersCase(string S1)
{
for (short i = 0; i < S1.length(); i++)
{
S1[i] = InvertLetterCase(S1[i]);
}
return S1;
}
void InvertAllLettersCase()
{
_Value = InvertAllLettersCase(_Value);
}
enum enWhatToCount { SmallLetters = 0, CapitalLetters = 1, All = 3 };
static short CountLetters(string S1, enWhatToCount WhatToCount = enWhatToCount::All)
{
if (WhatToCount == enWhatToCount::All)
{
return S1.length();
}
short Counter = 0;
for (short i = 0; i < S1.length(); i++)
{
if (WhatToCount == enWhatToCount::CapitalLetters && isupper(S1[i]))
Counter++;
if (WhatToCount == enWhatToCount::SmallLetters && islower(S1[i]))
Counter++;
}
return Counter;
}
static short CountCapitalLetters(string S1)
{
short Counter = 0;
for (short i = 0; i < S1.length(); i++)
{
if (isupper(S1[i]))
Counter++;
}
return Counter;
}
short CountCapitalLetters()
{
return CountCapitalLetters(_Value);
}
static short CountSmallLetters(string S1)
{
short Counter = 0;
for (short i = 0; i < S1.length(); i++)
{
if (islower(S1[i]))
Counter++;
}
return Counter;
}
short CountSmallLetters()
{
return CountSmallLetters(_Value);
}
static short CountSpecificLetter(string S1, char Letter, bool MatchCase = true)
{
short Counter = 0;
for (short i = 0; i < S1.length(); i++)
{
if (MatchCase)
{
if (S1[i] == Letter)
Counter++;
}
else
{
if (tolower(S1[i]) == tolower(Letter))
Counter++;
}
}
return Counter;
}
short CountSpecificLetter(char Letter, bool MatchCase = true)
{
return CountSpecificLetter(_Value, Letter, MatchCase);
}
static bool IsVowel(char Ch1)
{
Ch1 = tolower(Ch1);
return ((Ch1 == 'a') || (Ch1 == 'e') || (Ch1 == 'i') || (Ch1 == 'o') || (Ch1 == 'u'));
}
static short CountVowels(string S1)
{
short Counter = 0;
for (short i = 0; i < S1.length(); i++)
{
if (IsVowel(S1[i]))
Counter++;
}
return Counter;
}
short CountVowels()
{
return CountVowels(_Value);
}
static vector<string> Split(string S1, string Delim)
{
vector<string> vString;
short pos = 0;
string sWord; // define a string variable
// use find() function to get the position of the delimiters
while ((pos = S1.find(Delim)) != std::string::npos)
{
sWord = S1.substr(0, pos); // store the word
// if (sWord != "")
// {
vString.push_back(sWord);
//}
S1.erase(0, pos + Delim.length()); /* erase() until positon and move to next word. */
}
if (S1 != "")
{
vString.push_back(S1); // it adds last word of the string.
}
return vString;
}
vector<string> Split(string Delim)
{
return Split(_Value, Delim);
}
static string TrimLeft(string S1)
{
for (short i = 0; i < S1.length(); i++)
{
if (S1[i] != ' ')
{
return S1.substr(i, S1.length() - i);
}
}
return "";
}
void TrimLeft()
{
_Value = TrimLeft(_Value);
}
static string TrimRight(string S1)
{
for (short i = S1.length() - 1; i >= 0; i--)
{
if (S1[i] != ' ')
{
return S1.substr(0, i + 1);
}
}
return "";
}
void TrimRight()
{
_Value = TrimRight(_Value);
}
static string Trim(string S1)
{
return (TrimLeft(TrimRight(S1)));
}
void Trim()
{
_Value = Trim(_Value);
}
static string JoinString(vector<string> vString, string Delim)
{
string S1 = "";
for (string& s : vString)
{
S1 = S1 + s + Delim;
}
return S1.substr(0, S1.length() - Delim.length());
}
static string JoinString(string arrString[], short Length, string Delim)
{
string S1 = "";
for (short i = 0; i < Length; i++)
{
S1 = S1 + arrString[i] + Delim;
}
return S1.substr(0, S1.length() - Delim.length());
}
static string ReverseWordsInString(string S1)
{
vector<string> vString;
string S2 = "";
vString = Split(S1, " ");
// declare iterator
vector<string>::iterator iter = vString.end();
while (iter != vString.begin())
{
--iter;
S2 += *iter + " ";
}
S2 = S2.substr(0, S2.length() - 1); //remove last space.
return S2;
}
void ReverseWordsInString()
{
_Value = ReverseWordsInString(_Value);
}
static string ReplaceWord(string S1, string StringToReplace, string sRepalceTo, bool MatchCase = true)
{
vector<string> vString = Split(S1, " ");
for (string& s : vString)
{
if (MatchCase)
{
if (s == StringToReplace)
{
s = sRepalceTo;
}
}
else
{
if (LowerAllString(s) == LowerAllString(StringToReplace))
{
s = sRepalceTo;
}
}
}
return JoinString(vString, " ");
}
string ReplaceWord(string StringToReplace, string sRepalceTo)
{
return ReplaceWord(_Value, StringToReplace, sRepalceTo);
}
static string RemovePunctuations(string S1)
{
string S2 = "";
for (short i = 0; i < S1.length(); i++)
{
if (!ispunct(S1[i]))
{
S2 += S1[i];
}
}
return S2;
}
void RemovePunctuations()
{
_Value = RemovePunctuations(_Value);
}
};