Manipulate Strings in Dart

Jermaine Oppong
InstructorJermaine Oppong
Share this video with your friends

Social Share Links

Send Tweet

We will learn how to work with Strings using a variety of methods made available in the dart:core library. We will explore the top methods for working with String values.

Learn more about Strings at https://api.dartlang.org/stable/2.2.0/dart-core/String-class.html

Instructor: [00:00] The Dart toolbox contains utility methods for working with strings as part of the dart:core library. Use the contains method to check whether the specified text exists. You can pass in a regular expression to enhance your search.

[00:24] To check whether the string starts with a particular character or set of characters, use the startsWith method. To check whether the string ends with a particular character or set of characters, use the endsWith method.

[00:43] To convert the string to its uppercase and lowercase formats, use the toUpperCase and toLowerCase methods. Strings can also be split into an array of strings using the split method and passing it a delimiter.

[01:04] Use the splitMapJoin method to split a string into an array of strings, perform transformation on each item, and return the joined output. The first argument takes a pattern to match for. The second argument is a named parameter called onMatch, which allows us to perform transformation on our matched pattern.

[01:31] The third argument is a named parameter which allows us to perform transformations on non-matches. You can enable the multiline option for our regex if you wish to affect each line.

[01:49] To retrieve the positions of the first and last matches of a particular string, use the indexOf and lastIndexOf methods. Use the trim method to remove any leading and trailing white spaces. Here's what it looks like without using the trim method. You can pad a string using the padLeft and padRight methods if the string length is less than the specified value.

[02:29] Last but not least, you can replace a part of the string that matches the specified pattern. Here is how to do the same using a regular expression. If you wish to retain any captured groups from your regular expressions, use the replaceAllMapped method. Then we'll wrap the first captured group in the curved braces. This concludes our lesson.