DEV Community

Cover image for Top 10 String utility methods you should know (Dart) 🎯
Jermaine
Jermaine

Posted on • Updated on • Originally published at creativebracket.com

Top 10 String utility methods you should know (Dart) 🎯

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
Enter fullscreen mode Exit fullscreen mode

Let's start with:

1. contains()

This allows you to check whether the specified string exists:

str1.contains('rem'); // true
Enter fullscreen mode Exit fullscreen mode

2. startsWith()

This allows you to check whether the string starts with the specified characters:

str2.startsWith('Lorem'); // true
str3.startsWith('Noorem'); // false
Enter fullscreen mode Exit fullscreen mode

3. endsWith()

Checks whether the string ends with the specified characters:

str3.endsWith('ipsum'); // true
str3.endsWith('oopsum'); // false
Enter fullscreen mode Exit fullscreen mode

4. toLowerCase(), toUpperCase()

Converts the string to lowercase and uppercase formats:

str1.toLowerCase(); // lorem
str1.toUpperCase(); // LOREM
Enter fullscreen mode Exit fullscreen mode

5. split()

Splits the string at the matching pattern, returning a list of substrings:

str3.split('\n'); // ['Multi', 'Line', 'Lorem Lorem ipsum'];
Enter fullscreen mode Exit fullscreen mode

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

*/
Enter fullscreen mode Exit fullscreen mode

7. indexOf(), lastIndexOf()

Returns the position of the first and last matches of the given pattern:

str3.indexOf('rem'); // 13
str3.lastIndexOf('rem'); // 19
Enter fullscreen mode Exit fullscreen mode

Both methods also take in an optional parameter specifying the index to begin the search from:

str3.lastIndexOf('rem', 18); // 13
Enter fullscreen mode Exit fullscreen mode

8. trim()

Removes leading and trailing whitespaces:

"   $str2  ".trim(); // 'Lorem ipsum'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

10. replaceAll()

Replaces all substrings that match the specified pattern with the replacement string:

str2.replaceAll('e', 'é'); // Lorém
Enter fullscreen mode Exit fullscreen mode

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

  1. String class Documentation
  2. Free Dart screencasts on Egghead.io

Continue reading:


Top comments (6)

Collapse
 
thatwasawkward profile image
Ben Wolman

Thanks for this!

I really appreciate how Dart improves on JS.

Collapse
 
doblesesays profile image
Genessis Jimenez

Greet, thnks! I had never read about padLeft and padRight.

Collapse
 
megamattmiller profile image
Matt Miller

Somehow I never knew about padLeft() and padRight()! Thanks!

Collapse
 
revcode profile image
revcode

Wow! I missed padLeft and PadRight function.

Collapse
 
doblesesays profile image
Genessis Jimenez

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.

Collapse
 
creativ_bracket profile image
Jermaine

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?