-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHelper.cpp
More file actions
119 lines (93 loc) · 2.76 KB
/
StringHelper.cpp
File metadata and controls
119 lines (93 loc) · 2.76 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
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
#include "StringHelper.h"
#include <QDebug>
#include <QRegularExpression>
QString addPattern(QString value, QString pattern, int each){
QString result;
for (int i = 0; i < value.length() ; i++){
result.append(value[i]);
if ( (i + 1) % each == 0){
result.append(pattern);
}
}
return result;
}
/*
int main(int argc, char *argv[])
{
qDebug() << addChar("AAAAAAA", "BB", 2);
qDebug() << removePattern("abc-abc-abc-abc", "abc", 2);
qDebug() << removeEach("123456", 2);
qDebug() << removePosition("123", 2);
return 0;
}
*/
QString removePattern(QString value, QString pattern, int each){
QString result;
int count = 0;
QRegularExpression re(QRegularExpression::escape(pattern));
QRegularExpressionMatchIterator it = re.globalMatch(value);
int lastIndex = 0;
while (it.hasNext()){
QRegularExpressionMatch match = it.next();
count ++;
result += value.mid(lastIndex, match.capturedStart() - lastIndex);
if (count % each != 0){
result += match.captured();
}
lastIndex = match.capturedEnd();
}
result += value.mid(lastIndex);
return result;
}
QString removeEach(QString value, int each){
QString result;
if (each != 1){
result.append(value[0]);
}
for (int i = 1; i < value.length(); i++){
if (((i + 1) % each) == 0){
continue;
}
result.append(value[i]);
}
return result;
}
QString removePosition(QString value, int position){
QString result;
for (int i = 0; i < value.length(); i++){
if (i + 1 != position){
result.append(value[i]);
}
}
return result;
}
QString findPattern(QString input, QString pattern, int counter){
QRegularExpression regex(pattern);
QRegularExpressionMatchIterator match = regex.globalMatch(input);
QString result;
int current = 1;
while (match.hasNext()){
QRegularExpressionMatch instance = match.next();
if (current % counter == 0){
result += instance.captured(0) + "\n";
}
current += 1;
}
return result;
}
QString replacePattern(QString input, QString pattern, int counter, QString replacement){
QRegularExpression regex(pattern);
QRegularExpressionMatchIterator match = regex.globalMatch(input);
QString result = input;
int current = 1;
int offset = 0;
while (match.hasNext()){
QRegularExpressionMatch instance = match.next();
if (current % counter == 0){
result.replace(instance.capturedStart(0) + offset, instance.capturedLength(0), replacement);
offset += replacement.length() - instance.capturedLength(0);
}
current += 1;
}
return result;
}