Manipulate Strings in Dart

Jermaine Oppong
InstructorJermaine Oppong
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

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.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today