1+ import 'dart:math' ;
12import 'package:intl/intl.dart' ;
2- import 'dart:convert' show utf8, base64 ;
3+ import 'dart:convert' show base64, base64UrlEncode, utf8 ;
34
45class CommonStringUtils {
56 static final String _exceptionTag = 'Exception on StringUtils : ' ;
@@ -8,10 +9,12 @@ class CommonStringUtils {
89
910 CommonStringUtils ._();
1011
12+ /// Set a string value to [_mainStr] as the variable that should process.
1113 void construct (String mainStr) {
1214 this ._mainStr = mainStr;
1315 }
1416
17+ /// make a sub-string of given string by deleting [startStr] and [endStr] .
1518 String subStringBetween (String startStr, String endStr) {
1619 String result = '' ;
1720 try {
@@ -25,6 +28,7 @@ class CommonStringUtils {
2528 return result;
2629 }
2730
31+ /// make a [List<String>] of given string by deleting [startStr] and [endStr] .
2832 List <String > subStringsBetween (String startStr, String endStr) {
2933 List <String > results = [];
3034 _mainStr.split ('$endStr ' ).forEach ((element) {
@@ -36,6 +40,7 @@ class CommonStringUtils {
3640 return results;
3741 }
3842
43+ /// find [targetStr] in given string and return the sub-string after it.
3944 String subStringAfter (String targetStr, [bool lastIndex = false ]) {
4045 String result = '' ;
4146 try {
@@ -51,6 +56,7 @@ class CommonStringUtils {
5156 return result;
5257 }
5358
59+ /// find [targetStr] in given string and return the sub-string before it.
5460 String subStringBefore (String targetStr, [bool lastIndex = false ]) {
5561 String result = '' ;
5662 try {
@@ -63,35 +69,45 @@ class CommonStringUtils {
6369 return result;
6470 }
6571
72+ /// add [wordStr] in [index] of the given string.
6673 String insertAt (int index, String wordStr) {
6774 var selectStr = _mainStr[index];
6875 return _mainStr.replaceFirst ('$selectStr ' , '$selectStr ' + '$wordStr ' );
6976 }
7077
78+ /// remove the character in [index] of the given string.
7179 String removeAt (int index) {
7280 var selectStr = _mainStr[index];
7381 return _mainStr.replaceFirst ('$selectStr ' , '' );
7482 }
7583
84+ /// find the first [targetStr] in the given string and add the [word] to it.
85+ /// then return the result as string
7686 String insertAfter (String targetStr, String word) {
7787 return _mainStr.replaceFirst ('$targetStr ' , '$targetStr ' + '$word ' );
7888 }
7989
90+ /// find every [targetStr] in the given string and add the [word] to it.
91+ /// then return the result as string
8092 String insertAfterEvery (String targetStr, String word) {
8193 return _mainStr.replaceAll ('$targetStr ' , '$targetStr ' + '$word ' );
8294 }
8395
96+ /// find all the characters after [targetStr] and remove them from the given string.
8497 String removeAfter (String targetStr) {
8598 var tempTarget =
8699 _mainStr.substring (_mainStr.indexOf ('$targetStr ' ) + targetStr.length);
87100 return _mainStr.replaceFirst ('$tempTarget ' , '' );
88101 }
89102
103+ /// find all the characters before [targetStr] and remove them from the given string.
90104 String removeBefore (String targetStr) {
91105 var tempTarget = _mainStr.substring (0 , _mainStr.indexOf ('$targetStr ' ));
92106 return _mainStr.replaceFirst ('$tempTarget ' , '' );
93107 }
94108
109+ /// return true if the string is castable to [int] type,
110+ /// if it's not, return false.
95111 bool isNumericInt () {
96112 final validCharacters = RegExp (r'^[0-9]+$' );
97113
@@ -102,13 +118,17 @@ class CommonStringUtils {
102118 }
103119 }
104120
121+ /// return true if the string is castable to [double] type,
122+ /// if it's not, return false.
105123 bool isNumericDouble () {
106124 if (_mainStr.isEmpty) {
107125 return false ;
108126 }
109127 return double .tryParse (_mainStr) != null ;
110128 }
111129
130+ /// return true if all the characters of the given string is alphabetic characters,
131+ /// if it's not, return false.
112132 bool isAlphabetic (bool withSpace) {
113133 final validCharacters =
114134 withSpace ? RegExp (r'^[a-zA-Z ]+$' ) : RegExp (r'^[a-zA-Z]+$' );
@@ -120,6 +140,8 @@ class CommonStringUtils {
120140 }
121141 }
122142
143+ /// return true if all the characters of the given string is uppercase,
144+ /// if it's not, return false.
123145 bool isUpperCase () {
124146 final validCharacters = _mainStr.toUpperCase ();
125147 if (_mainStr == validCharacters &&
@@ -130,6 +152,8 @@ class CommonStringUtils {
130152 }
131153 }
132154
155+ /// return true if all the characters of the given string is combination of alphabetic and numeric characters,
156+ /// if it's not, return false.
133157 bool isAlphaNumeric (bool withSpace) {
134158 final validCharacters =
135159 withSpace ? RegExp (r'^[a-zA-Z0-9 ]+$' ) : RegExp (r'^[a-zA-Z0-9]+$' );
@@ -141,6 +165,8 @@ class CommonStringUtils {
141165 }
142166 }
143167
168+ /// return true if the given string is blank
169+ /// if it's not, return false.
144170 bool isBlank () {
145171 final validCharacters = RegExp (r'^[ ]+$' );
146172
@@ -151,6 +177,8 @@ class CommonStringUtils {
151177 }
152178 }
153179
180+ /// return true if all the characters of the given string contains special characters,
181+ /// if it's not, return false.
154182 bool isContainSpecialChar () {
155183 final validCharacters = RegExp (r'^[a-zA-Z0-9 ]+$' );
156184
@@ -161,6 +189,8 @@ class CommonStringUtils {
161189 }
162190 }
163191
192+ /// return true if all the characters of the given string is valid characters,
193+ /// if it's not, return false.
164194 bool isValidCharacters (RegExp validCharacters) {
165195 try {
166196 if (validCharacters.hasMatch (_mainStr)) {
@@ -174,6 +204,8 @@ class CommonStringUtils {
174204 }
175205 }
176206
207+ /// convert all the english numbers to persian in the given string,
208+ /// then return the result.
177209 String convertEnglishNumberToPersian () {
178210 const english = ['0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ];
179211 const farsi = ['۰' , '۱' , '۲' , '۳' , '۴' , '۵' , '۶' , '۷' , '۸' , '۹' ];
@@ -184,6 +216,8 @@ class CommonStringUtils {
184216 return _mainStr;
185217 }
186218
219+ /// convert all the persian numbers to english in the given string,
220+ /// then return the result.
187221 String convertPersianNumberToEnglish () {
188222 const english = ['0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ];
189223 const farsi = ['۰' , '۱' , '۲' , '۳' , '۴' , '۵' , '۶' , '۷' , '۸' , '۹' ];
@@ -194,6 +228,8 @@ class CommonStringUtils {
194228 return _mainStr;
195229 }
196230
231+ /// add comma character to the given string to make the money format,
232+ /// then return the result string.
197233 String convertToMoneyFormat () {
198234 final formatter = new NumberFormat ("#,###" , "en_US" );
199235 if (isNumericDouble ()) {
@@ -206,30 +242,37 @@ class CommonStringUtils {
206242 }
207243 }
208244
245+ /// return the characters count of the given string
209246 int countWords (String wordStr) {
210247 int wordCount = 0 ;
211248 wordCount = '$wordStr ' .allMatches ('$_mainStr ' ).length;
212249 return wordCount;
213250 }
214251
252+ /// return the lines count of the given string
215253 int countLines () {
216254 int lineCount = 0 ;
217255 lineCount = '\n ' .allMatches ('$_mainStr ' ).length;
218256 return lineCount + 1 ;
219257 }
220258
259+ /// reverse string from the end to the start
221260 String reverse () {
222261 return _mainStr.split ('' ).reversed.join ();
223262 }
224263
264+ /// reverse the words of the given string
225265 String reverseWords () {
226266 return _mainStr.split (' ' ).reversed.join (' ' );
227267 }
228268
269+ /// remove the blank lines from the given string,
270+ /// the return the result string.
229271 String removeBlankLines () {
230272 return _mainStr.replaceAll (new RegExp (r'(?:[\t ]*(?:\r?\n|\r))+' ), '\n ' );
231273 }
232274
275+ /// remove the lines that contains [wordStr] from the given string.
233276 String removeLinesThatContain (String wordStr) {
234277 var lines = _mainStr.split ('\n ' );
235278 for (var lineItem in _mainStr.split ('\n ' )) {
@@ -240,6 +283,8 @@ class CommonStringUtils {
240283 return lines.join ('\n ' );
241284 }
242285
286+ /// return true if the given string is valid Email,
287+ /// if it's not, return false.
243288 bool isEmail () {
244289 var emailValidation = RegExp (
245290 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])?)*$" );
@@ -250,10 +295,14 @@ class CommonStringUtils {
250295 }
251296 }
252297
298+ /// convert the given string to [base64]
299+ /// then return the result string.
253300 String convertToBase64 () {
254301 return base64.encode (utf8.encode (_mainStr));
255302 }
256303
304+ /// convert the given [base64] to string
305+ /// then return the result string.
257306 String convertBase64ToString () {
258307 return utf8.decode (base64.decode (_mainStr));
259308 }
@@ -262,7 +311,22 @@ class CommonStringUtils {
262311 return utf8.encode ('$_mainStr ' );
263312 }
264313
314+ /// wrap the given string between the [wrapWith]
265315 String wrap (String wrapWith) {
266316 return '$wrapWith $_mainStr $wrapWith ' ;
267317 }
318+
319+ /// return true if the given string is valid URL,
320+ /// if it's not, return false.
321+ bool isValidUrl () {
322+ if (_mainStr.isEmpty) return false ;
323+ return Uri .tryParse (_mainStr)? .hasAbsolutePath ?? false ;
324+ }
325+
326+ /// generate a secure random string
327+ String getRandomString (int len) {
328+ var random = Random .secure ();
329+ var values = List <int >.generate (len, (i) => random.nextInt (255 ));
330+ return base64UrlEncode (values);
331+ }
268332}
0 commit comments