Committed by
GitHub
Merge pull request #2547 from anasfik/master
capitalizeAllWordsFirstLetter() method to capitalize only first letter in String words
Showing
3 changed files
with
78 additions
and
0 deletions
| @@ -124,4 +124,8 @@ extension GetStringUtils on String { | @@ -124,4 +124,8 @@ extension GetStringUtils on String { | ||
| 124 | final path = startsWith('/') ? this : '/$this'; | 124 | final path = startsWith('/') ? this : '/$this'; |
| 125 | return GetUtils.createPath(path, segments); | 125 | return GetUtils.createPath(path, segments); |
| 126 | } | 126 | } |
| 127 | + | ||
| 128 | + /// capitalize only first letter in String words to upper case | ||
| 129 | + String capitalizeAllWordsFirstLetter() => | ||
| 130 | + GetUtils.capitalizeAllWordsFirstLetter(this); | ||
| 127 | } | 131 | } |
| @@ -595,6 +595,44 @@ class GetUtils { | @@ -595,6 +595,44 @@ class GetUtils { | ||
| 595 | return numericOnlyStr; | 595 | return numericOnlyStr; |
| 596 | } | 596 | } |
| 597 | 597 | ||
| 598 | + /// Capitalize only the first letter of each word in a string | ||
| 599 | + /// Example: getx will make it easy => Getx Will Make It Easy | ||
| 600 | + /// Example 2 : this is an example text => This Is An Example Text | ||
| 601 | + static String capitalizeAllWordsFirstLetter(String s) { | ||
| 602 | + String lowerCasedString = s.toLowerCase(); | ||
| 603 | + String stringWithoutExtraSpaces = lowerCasedString.trim(); | ||
| 604 | + | ||
| 605 | + if (stringWithoutExtraSpaces.isEmpty) { | ||
| 606 | + return ""; | ||
| 607 | + } | ||
| 608 | + if (stringWithoutExtraSpaces.length == 1) { | ||
| 609 | + return stringWithoutExtraSpaces.toUpperCase(); | ||
| 610 | + } | ||
| 611 | + | ||
| 612 | + List<String> stringWordsList = stringWithoutExtraSpaces.split(" "); | ||
| 613 | + List<String> capitalizedWordsFirstLetter = stringWordsList | ||
| 614 | + .map( | ||
| 615 | + (word) { | ||
| 616 | + if (word.trim().isEmpty) return ""; | ||
| 617 | + return word.trim(); | ||
| 618 | + }, | ||
| 619 | + ) | ||
| 620 | + .where( | ||
| 621 | + (word) => word != "", | ||
| 622 | + ) | ||
| 623 | + .map( | ||
| 624 | + (word) { | ||
| 625 | + if (word.startsWith(RegExp(r'[\n\t\r]'))) { | ||
| 626 | + return word; | ||
| 627 | + } | ||
| 628 | + return word[0].toUpperCase() + word.substring(1).toLowerCase(); | ||
| 629 | + }, | ||
| 630 | + ) | ||
| 631 | + .toList(); | ||
| 632 | + String finalResult = capitalizedWordsFirstLetter.join(" "); | ||
| 633 | + return finalResult; | ||
| 634 | + } | ||
| 635 | + | ||
| 598 | static bool hasMatch(String? value, String pattern) { | 636 | static bool hasMatch(String? value, String pattern) { |
| 599 | return (value == null) ? false : RegExp(pattern).hasMatch(value); | 637 | return (value == null) ? false : RegExp(pattern).hasMatch(value); |
| 600 | } | 638 | } |
| @@ -16,6 +16,42 @@ void main() { | @@ -16,6 +16,42 @@ void main() { | ||
| 16 | expect(text.isNum, false); | 16 | expect(text.isNum, false); |
| 17 | }); | 17 | }); |
| 18 | 18 | ||
| 19 | + test('var.capitalizeAllWordsFirstLetter()', () { | ||
| 20 | + final List<String> sentences = [ | ||
| 21 | + "getx", | ||
| 22 | + "this is an example sentence", | ||
| 23 | + "this is an example sentence with a number 5", | ||
| 24 | + "this is an example sentence with a number 5 and a special character #", | ||
| 25 | + "this is an example sentence with a number 5 and a special character # and b letter C", | ||
| 26 | + " emm, lemme think !", | ||
| 27 | + "Bro, $letters is a good word", | ||
| 28 | + "THIS IS A SENTENCE WITH ALL CAPITAL LETTERS", | ||
| 29 | + "" | ||
| 30 | + ]; | ||
| 31 | + expect(text.capitalizeAllWordsFirstLetter(), "Oi"); | ||
| 32 | + expect(digit.capitalizeAllWordsFirstLetter(), "5"); | ||
| 33 | + expect(specialCaracters.capitalizeAllWordsFirstLetter(), "#\$!%@"); | ||
| 34 | + expect(alphaNumeric.capitalizeAllWordsFirstLetter(), "123asd"); | ||
| 35 | + expect(numbers.capitalizeAllWordsFirstLetter(), "123"); | ||
| 36 | + expect(letters.capitalizeAllWordsFirstLetter(), "Foo"); | ||
| 37 | + expect(sentences[0].capitalizeAllWordsFirstLetter(), "Getx"); | ||
| 38 | + expect(sentences[1].capitalizeAllWordsFirstLetter(), | ||
| 39 | + "This Is An Example Sentence"); | ||
| 40 | + expect(sentences[2].capitalizeAllWordsFirstLetter(), | ||
| 41 | + "This Is An Example Sentence With A Number 5"); | ||
| 42 | + expect(sentences[3].capitalizeAllWordsFirstLetter(), | ||
| 43 | + "This Is An Example Sentence With A Number 5 And A Special Character #"); | ||
| 44 | + expect(sentences[4].capitalizeAllWordsFirstLetter(), | ||
| 45 | + "This Is An Example Sentence With A Number 5 And A Special Character # And B Letter C"); | ||
| 46 | + expect( | ||
| 47 | + sentences[5].capitalizeAllWordsFirstLetter(), "Emm, Lemme Think !"); | ||
| 48 | + expect(sentences[6].capitalizeAllWordsFirstLetter(), | ||
| 49 | + "Bro, Foo Is A Good Word"); | ||
| 50 | + expect(sentences[7].capitalizeAllWordsFirstLetter(), | ||
| 51 | + "This Is A Sentence With All Capital Letters"); | ||
| 52 | + expect(sentences[8].capitalizeAllWordsFirstLetter(), ""); | ||
| 53 | + }); | ||
| 54 | + | ||
| 19 | test('var.isNumericOnly', () { | 55 | test('var.isNumericOnly', () { |
| 20 | expect(numbers.isNumericOnly, true); | 56 | expect(numbers.isNumericOnly, true); |
| 21 | expect(letters.isNumericOnly, false); | 57 | expect(letters.isNumericOnly, false); |
-
Please register or login to post a comment