string_extensions 0.4.0 string_extensions: ^0.4.0 copied to clipboard
Useful String extensions to save you time in production. Feel free to contribute with PR.
Current Methods #
- You can use
>, >=, <, <=
operators, to compareString
lengths. Also-
to subtract a text from aString
String s1 = 'esentis';
String s2 = 'dev';
print(s1 > s2); // prints true
print(s1 >= s2); // prints true
print(s1 < s2); // prints false
print(s1 <= s2); // prints false
print(s1-'tis'); // prints 'esen'
- isGuid
String foo = '6d64-4396-8547-1ec1b86e081e'
bool isGuid = foo.isGuid; // returns false
- isNull
String foo;
bool isNull = foo.isNull; // returns true
- isUrl
String foo = 'esentis';
bool isUrl = foo.isUrl; // 'false';
- isDate
String foo = 'esentis';
bool isDate = foo.isDate; // 'false';
- isMail
String foo = 'esentis@esentis.com';
bool isMail = foo.isMail; // returns true
String foo = 'esentis@esentis'
bool isMail = foo.isMail; // returns false
- isIpv4
String foo = '192.168.1.14';
bool isIpv4 = foo.isIpv4; // returns true
String foo = '192.168.1.14.150.1225';
bool isIpv4 = foo.isIpv4; // returns false
- isIpv6
String foo = '2001:0db8:85a3:0000:0000:8a2e:0370:7334';
bool isIpv6 = foo.isIpv6; // returns true
String foo = '92.168.1.14';
bool isIpv6 = foo.isIpv6; // returns false
- isNumber
String foo = '44';
bool isNumber = foo.isNumber; // returns true
String foo = '92.168.1.14';
bool isNumber = foo.isNumber; // returns false
- isIn()
String foo = '6d64-4396-8547-1ec1b86e081e';
var iterable = ['fff','gasd'];
bool isIn = foo.isIn(iterable); // returns false
- toArray
String foo = 'abracadabra';
List<String> fooArray = foo.toArray; // returns '[a,b,r,a,c,a,d,a,b,r,a]'
- toCamelCase
String foo = 'Find max of array';
String camelCase = foo.toCamelCase // returns 'findMaxOfArray'
- toTitleCase
String foo = 'hello world';
String fooTitled = foo.toTitleCase; // returns 'Hello World'
- toSlug
String foo = 'hello world';
String fooSlug = foo.toSlug; // returns 'hello_world'
- toNum()
String foo = '5.5';
double fooNum = foo.toNum(); // returns 5.5
String foo2 = '5';
int fooNum2 = foo.toNum(); // returns 5
String foo3 = '5f';
var fooNum2 = foo.toNum(); // returns null
- toInt()
String foo = '5.6';
int fooInt = foo.toInt(); // returns 5
String foo2 = '5';
int fooNum2 = foo.toInt(); // returns 5
String foo3 = '5f';
var fooNum2 = foo.toInt(); // returns null
- toDouble()
String foo = '5';
double fooDouble = foo.toDouble(); // returns 5.0
String foo = '5.5';
double fooDouble = foo.toDouble(); // returns 5.5
String foo = '5f';
var fooDouble = foo.toDouble(); // returns null
- onlyLatin
String foo = '4*%^55/es4e5523nt1is';
String letters = foo.onlyLatin; // returns 'esentis';
- onlyGreek
String foo = '4*%^55/σοφί4e5523nt1isα';
String letters = foo.onlyLatin; // returns 'σοφία';
- onlyNumbers
String foo = '4*%^55/es4e5523nt1is';
String onyNumbers = foo.onlyNumbers; // returns '455455231'
- readTime({int wordsPerMinute})
String foo = 'Hello dear friend how you doing ?';
int readTime = foo.readTime(); // returns 3 seconds.
- removeNumbers
String foo = 'es4e5523nt1is';
String noNums = foo.removeNumbers; // returns 'esentis'
String foo = '1244e*s*4e*5523n*t*1i*s';
String noNums = foo.removeNumbers; // returns 'e*s*e*n*t*i*s'
- countWords
String foo = 'Hello dear friend how you doing ?';
int count = foo.countWords; // returns 6 words.
- capitalize
String foo = 'hAckErrR';
String cFoo = foo.capitalize; // returns 'Hackerrr'.
- removeLetters
String foo = 'es4e5523nt1is';
String noLetters = foo.removeLetters; // returns '455231'
String foo = '1244e*s*4e*5523n*t*1i*s';
String noLetters = foo.removeLetters;// returns '1244**4*5523**1*'
- charOccurences
String foo = 'esentis';
List occurences = foo.charOccurences; // returns '[{e:2},{i:1},{n:1},{s:2},]'
- mostFrequent
String foo = 'Hello World';
String mostFrequent = foo.mostFrequent; // returns 'l'
- reverse
String foo = 'Hello World';
String reversed = foo.reverse; // returns 'dlrow olleH'
- first()
String foo = 'hello world';
String firstChars = foo.first(); // returns 'h'
- last()
String foo = 'hello world';
String lastChars = foo.last(); // returns 'd'
- replaceGreek
String foo = 'Αριστοτέλης';
String fooReplaced = foo.replaceGreek; // returns 'aristotelis'
- findPatterns({required String pattern})
String foo = 'abracadabra';
String fooOccs = foo.findPattern(pattern:'abr'); // returns '[0, 7]'
- stripHtml
String html = '<script>Hacky hacky.</script> <p>Here is some text. <span class="bold">This is bold. </span></p>';
String stripped = foo.stripHtml; // returns 'Hacky hacky. Here is some text. This is bold.'
- repeat(int x)
String string = 'foo';
String stripped = foo.repeat(2); // returns 'foofoo'
- squeeze(String x)
String foo = 'foofoofoofoofoo';
String fooSqueezed = foo.squeeze('o'); // 'fofofofofo';
- hasSameCharacters
String foo1 = 'ttttttt';
bool hasSame1 = foo.hasSameCharacters; // true;
String foo = 'ttttttt12'
bool hasSame2 = foo.hasSameCharacters; // false;
- shuffle
String foo = 'esentis';
String fooSqueezed = foo.shuffle; // 'teienss';
- getLevenshtein(String word)
String foo = 'esentis';
int distance = foo.getLevenshtein('esen'); // '3';
- charCount(String char)
String foo = 'esentis';
int distance = foo.charCount('e'); // '2';
- formatWithMask(String mask)
String string = 'esentisgreece';
String mask = 'Hello ####### you are from ######';
String masked = string3.formatWithMask(mask); // returns 'Hello esentis you are from greece'
- removeFirst(int n)
String foo = 'esentis';
String newFoo = foo.removeFirst(3); // 'ntis';
- removeLast(int n)
String foo = 'esentis';
String newFoo = foo.removeLast(3); // 'esen';
- maxChars(int n)
String foo = 'esentis';
String newFoo = foo.maxChars(3); // 'esen';
- reverseSlash(int direction)
String foo1 = 'C:/Documents/user/test';
String revFoo1 = foo1.reverseSlash(0); // returns 'C:\Documents\user\test'
String foo2 = 'C:\\Documents\\user\\test';
String revFoo2 = foo1.reverseSlash(1); // returns 'C:/Documents/user/test'
- charAt(int index)
String foo1 = 'esentis';
String char1 = foo1.charAt(0); // returns 'e'
- ifEmpty(Function act)
String foo1 = '';
foo1.ifEmpty(()=>'string is empty');
- ifNull(Function act)
String foo1;
foo1.ifEmpty(()=>'string is null');
- append(String suffix)
String foo1 = 'Hello';
foo1.append(' World'); // returns 'Hello World'
- prepend(String prefix)
String foo1 = 'World';
foo1.prepend('Hello'); // returns 'Hello World'
- toPriceAmount({String? currencySymbol})
String? s2 = '123333333';
String formatted = s2.toPriceAmount(currencySymbol: '€'); // returns '123.333.333,00 €'
- getDayFromDate({String locale})
String date = '2021-10-23';
String day = date.getDayFromDate(); // returns 'Saturday'
String grDay = date.getDayFromDate(locale:'el'); // returns 'Σάββατο'
- getMonthFromDate({String locale})
String date = '2021-10-23';
String month = date.getMonthFromDate(); // returns 'Octomber'
String grMonth = date.getMonthFromDate(locale:'el'); // returns 'Οκτωμβρίου'
- firstDayOfMonth({String locale})
String date = '2021-10-23';
String day = date.firstDayOfMonth(); // returns 'Friday'
String grDay = date.firstDayOfMonth(locale:'el'); // returns 'Παρασκευή'
- lastDayOfMonth({String locale})
String date = '2021-10-23';
String day = date.lastDayOfMonth(); // returns 'Sunday'
String grDay = date.lastDayOfMonth(locale:'el'); // returns 'Κυριακή'
- leftOf(String char)
String t = 'peanutbutter';
String left = date.leftOf('butter'); // returns 'peanut'
- rightOf(String char)
String t = 'peanutbutter';
String left = date.rightOf('peanut'); // returns 'butter'
- truncate(int maxChars)
String t = 'esentis';
String small = t.truncate(4); // returns 'esen...'
- truncateMiddle(int maxChars)
String t = 'esentis';
String small = t.truncateMiddle(2); // returns 'e...n'
- quote
String t = 'esentis';
String quote = t.quote; // returns "esentis"
- trimAll
String t = ' is this reality ?';
String quote = t.trimAll; // returns "is this reality ?"