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

Jermaine Oppong
codeburst
Published in
2 min readAug 21, 2018
Background photo by XiaoXiao Sun on Unsplash

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 begin with these…

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 then into a new string:

str3.splitMapJoin(RegExp(r'^', multiLine: true), // match line start
onMatch: (m) => '**${m.group(0)} ', // add asterisk to line start
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 a 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 than 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, share and follow me 😍 for more articles on Dart. Thanks so much.

Further learning

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

✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.

--

--

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Jermaine Oppong

Christian | Web Developer | Egghead.io instructor | Sharing exclusive Dart content on my Blog → https://creativebracket.com | @creativ_bracket

Responses (1)

What are your thoughts?