In today's string of articles on Top 10 methods, we will look at some useful utility methods that come with the String
type.
You can use these methods combined with inbuilt support for template literals, allowing effective manipulation of strings:
var str1 = 'Lorem';
var str2 = '$str1 ipsum'; // String interpolation
var str3 = '''Multi
Line
$str1 $str2'''; // Multi-line strings
Let's start with:
1. contains()
This allows you to check whether the specified string exists:
str1.contains('rem'); // true
2. startsWith()
This allows you to check whether the string starts with the specified characters:
str2.startsWith('Lorem'); // true
str3.startsWith('Noorem'); // false
3. endsWith()
Checks whether the string ends with the specified characters:
str3.endsWith('ipsum'); // true
str3.endsWith('oopsum'); // false
4. toLowerCase(), toUpperCase()
Converts the string to lowercase and uppercase formats:
str1.toLowerCase(); // lorem
str1.toUpperCase(); // LOREM
5. split()
Splits the string at the matching pattern, returning a list of substrings:
str3.split('\n'); // ['Multi', 'Line', 'Lorem Lorem ipsum'];
6. splitMapJoin()
Splits the string, converts each list item, and combines them into a new string:
str3.splitMapJoin(RegExp(r'^', multiLine: true), // Matches the beginning of each line
onMatch: (m) => '**${m.group(0)} ', // Adds asterisk to the line beginning
onNonMatch: (n) => n); // Leaves non matches as is
/*
Output:
** Multi
** Line
** Lorem Lorem ipsum
*/
7. indexOf(), lastIndexOf()
Returns the position of the first and last matches of the given pattern:
str3.indexOf('rem'); // 13
str3.lastIndexOf('rem'); // 19
Both methods also take in an optional parameter specifying the index to begin the search from:
str3.lastIndexOf('rem', 18); // 13
8. trim()
Removes leading and trailing whitespaces:
" $str2 ".trim(); // 'Lorem ipsum'
9. padLeft(), padRight()
Pads the string on the left and right with the given padding if the string is less that the specified length:
str1.padLeft(8, 'x'); // xxLorem
str1.padRight(8, 'x'); // Loremxx
10. replaceAll()
Replaces all substrings that match the specified pattern with the replacement string:
str2.replaceAll('e', 'é'); // Lorém
Conclusion
I hope this was insightful and if this is your first exposure to Dart, read my first steps tutorial to grasp the basics. The code snippets for this article are available on DartPad.
Like and follow me 😍 for more articles on Dart. Thanks so much.
Quicklinks
Continue reading:
Top comments (6)
Thanks for this!
I really appreciate how Dart improves on JS.
Greet, thnks! I had never read about padLeft and padRight.
Somehow I never knew about
padLeft()
andpadRight()
! Thanks!Wow! I missed padLeft and PadRight function.
I tried to use the contains method, but I get an error that it is not a property of the string type... Maybe it's just for the String type. i ended up using the endsWith method.
Hi Genessis, it seems you may be running this in a JS environment. Did you try out the DartPad link at the end of the article?