-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_utils.dart
324 lines (279 loc) · 9.6 KB
/
string_utils.dart
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
import 'package:intl/intl.dart';
import 'dart:convert' show base64, utf8;
class CommonStringUtils {
static final String _exceptionTag = 'Exception on StringUtils : ';
static final CommonStringUtils stringUtils = CommonStringUtils._();
String _mainStr = '';
CommonStringUtils._();
/// Set a string value to [_mainStr] as the variable that should process.
void construct(String mainStr) {
this._mainStr = mainStr;
}
/// make a sub-string of given string by deleting [startStr] and [endStr].
String subStringBetween(String startStr, String endStr) {
String result = '';
try {
final startIndex = _mainStr.indexOf(startStr);
final endIndex = _mainStr.indexOf(endStr, startIndex + startStr.length);
result = _mainStr.substring(startIndex + startStr.length, endIndex);
} catch (exc) {
print('$_exceptionTag$exc');
}
return result;
}
/// make a [List<String>] of given string by deleting [startStr] and [endStr].
List<String> subStringsBetween(String startStr, String endStr) {
List<String> results = [];
_mainStr.split('$endStr').forEach((element) {
if (element.isNotEmpty) {
results.add(
element.substring(element.indexOf('$startStr') + startStr.length));
}
});
return results;
}
/// find [targetStr] in given string and return the sub-string after it.
String subStringAfter(String targetStr, [bool lastIndex = false]) {
String result = '';
try {
lastIndex
? result = _mainStr
.substring(_mainStr.lastIndexOf('$targetStr') + targetStr.length)
: result = _mainStr
.substring(_mainStr.indexOf('$targetStr') + targetStr.length);
} catch (exc) {
print('$_exceptionTag$exc');
}
return result;
}
/// find [targetStr] in given string and return the sub-string before it.
String subStringBefore(String targetStr, [bool lastIndex = false]) {
String result = '';
try {
lastIndex
? result = _mainStr.substring(0, _mainStr.lastIndexOf('$targetStr'))
: result = _mainStr.substring(0, _mainStr.indexOf('$targetStr'));
} catch (exc) {
print('$_exceptionTag$exc');
}
return result;
}
/// add [wordStr] in [index] of the given string.
String insertAt(int index, String wordStr) {
var selectStr = _mainStr[index];
return _mainStr.replaceFirst('$selectStr', '$selectStr' + '$wordStr');
}
/// remove the character in [index] of the given string.
String removeAt(int index) {
var selectStr = _mainStr[index];
return _mainStr.replaceFirst('$selectStr', '');
}
/// find the first [targetStr] in the given string and add the [word] to it.
/// then return the result as string
String insertAfter(String targetStr, String word) {
return _mainStr.replaceFirst('$targetStr', '$targetStr' + '$word');
}
/// find every [targetStr] in the given string and add the [word] to it.
/// then return the result as string
String insertAfterEvery(String targetStr, String word) {
return _mainStr.replaceAll('$targetStr', '$targetStr' + '$word');
}
/// find all the characters after [targetStr] and remove them from the given string.
String removeAfter(String targetStr) {
var tempTarget =
_mainStr.substring(_mainStr.indexOf('$targetStr') + targetStr.length);
return _mainStr.replaceFirst('$tempTarget', '');
}
/// find all the characters before [targetStr] and remove them from the given string.
String removeBefore(String targetStr) {
var tempTarget = _mainStr.substring(0, _mainStr.indexOf('$targetStr'));
return _mainStr.replaceFirst('$tempTarget', '');
}
/// return true if the string is castable to [int] type,
/// if it's not, return false.
bool isNumericInt() {
final validCharacters = RegExp(r'^[0-9]+$');
if (validCharacters.hasMatch(_mainStr)) {
return true;
} else {
return false;
}
}
/// return true if the string is castable to [double] type,
/// if it's not, return false.
bool isNumericDouble() {
if (_mainStr.isEmpty) {
return false;
}
return double.tryParse(_mainStr) != null;
}
/// return true if all the characters of the given string is alphabetic characters,
/// if it's not, return false.
bool isAlphabetic(bool withSpace) {
final validCharacters =
withSpace ? RegExp(r'^[a-zA-Z ]+$') : RegExp(r'^[a-zA-Z]+$');
if (validCharacters.hasMatch(_mainStr)) {
return true;
} else {
return false;
}
}
/// return true if all the characters of the given string is uppercase,
/// if it's not, return false.
bool isUpperCase() {
final validCharacters = _mainStr.toUpperCase();
if (_mainStr == validCharacters &&
RegExp(r'^[a-zA-Z]+').hasMatch(_mainStr)) {
return true;
} else {
return false;
}
}
/// return true if all the characters of the given string is combination of alphabetic and numeric characters,
/// if it's not, return false.
bool isAlphaNumeric(bool withSpace) {
final validCharacters =
withSpace ? RegExp(r'^[a-zA-Z0-9 ]+$') : RegExp(r'^[a-zA-Z0-9]+$');
if (validCharacters.hasMatch(_mainStr)) {
return true;
} else {
return false;
}
}
/// return true if the given string is blank
/// if it's not, return false.
bool isBlank() {
final validCharacters = RegExp(r'^[ ]+$');
if (validCharacters.hasMatch(_mainStr)) {
return true;
} else {
return false;
}
}
/// return true if all the characters of the given string contains special characters,
/// if it's not, return false.
bool isContainSpecialChar() {
final validCharacters = RegExp(r'^[a-zA-Z0-9 ]+$');
if (!validCharacters.hasMatch(_mainStr)) {
return true;
} else {
return false;
}
}
/// return true if all the characters of the given string is valid characters,
/// if it's not, return false.
bool isValidCharacters(RegExp validCharacters) {
try {
if (validCharacters.hasMatch(_mainStr)) {
return true;
} else {
return false;
}
} catch (exc) {
print('$_exceptionTag$exc');
return false;
}
}
/// convert all the english numbers to persian in the given string,
/// then return the result.
String convertEnglishNumberToPersian() {
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const farsi = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
for (int i = 0; i < english.length; i++) {
_mainStr = _mainStr.replaceAll(english[i], farsi[i]);
}
return _mainStr;
}
/// convert all the persian numbers to english in the given string,
/// then return the result.
String convertPersianNumberToEnglish() {
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const farsi = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
for (int i = 0; i < farsi.length; i++) {
_mainStr = _mainStr.replaceAll(farsi[i], english[i]);
}
return _mainStr;
}
/// add comma character to the given string to make the money format,
/// then return the result string.
String convertToMoneyFormat() {
final formatter = new NumberFormat("#,###", "en_US");
if (isNumericDouble()) {
if (isNumericInt()) {
return formatter.format(int.parse(_mainStr));
}
return formatter.format(double.parse(_mainStr));
} else {
return _mainStr;
}
}
/// return the characters count of the given string
int countWords(String wordStr) {
int wordCount = 0;
wordCount = '$wordStr'.allMatches('$_mainStr').length;
return wordCount;
}
/// return the lines count of the given string
int countLines() {
int lineCount = 0;
lineCount = '\n'.allMatches('$_mainStr').length;
return lineCount + 1;
}
/// reverse string from the end to the start
String reverse() {
return _mainStr.split('').reversed.join();
}
/// reverse the words of the given string
String reverseWords() {
return _mainStr.split(' ').reversed.join(' ');
}
/// remove the blank lines from the given string,
/// the return the result string.
String removeBlankLines() {
return _mainStr.replaceAll(new RegExp(r'(?:[\t ]*(?:\r?\n|\r))+'), '\n');
}
/// remove the lines that contains [wordStr] from the given string.
String removeLinesThatContain(String wordStr) {
var lines = _mainStr.split('\n');
for (var lineItem in _mainStr.split('\n')) {
if (lineItem.contains('$wordStr')) {
lines.remove(lineItem);
}
}
return lines.join('\n');
}
/// return true if the given string is valid Email,
/// if it's not, return false.
bool isEmail() {
var emailValidation = RegExp(
r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$");
if (emailValidation.hasMatch(_mainStr)) {
return true;
} else {
return false;
}
}
/// convert the given string to [base64]
/// then return the result string.
String convertToBase64() {
return base64.encode(utf8.encode(_mainStr));
}
/// convert the given [base64] to string
/// then return the result string.
String convertBase64ToString() {
return utf8.decode(base64.decode(_mainStr));
}
List<int> convertToUTF8() {
return utf8.encode('$_mainStr');
}
/// wrap the given string between the [wrapWith].
String wrap(String wrapWith) {
return '$wrapWith$_mainStr$wrapWith';
}
/// return true if the given string is valid URL,
/// if it's not, return false.
bool isValidUrl() {
if (_mainStr.isEmpty) return false;
return Uri.tryParse(_mainStr)?.hasAbsolutePath ?? false;
}
}