Top 10 String utility methods you should know (Dart) 🎯
Tags: dart, webdev, beginners, showdev
Published: 2018-08-09 18:57:28

---
title: Top 10 String utility methods you should know (Dart) 🎯
published: true
description: Learn how to work with Strings in Dart
tags: dart, webdev, beginners, showdev
cover_image: https://thepracticaldev.s3.amazonaws.com/i/n3p7ycl62xuq9rdk5gbq.png
canonical_url: https://creativebracket.com/top-10-string-utility-methods-you-should-know/
---

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:

```dart
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:

```dart
str1.contains('rem'); // true
```

## 2. startsWith()

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

```dart
str2.startsWith('Lorem'); // true
str3.startsWith('Noorem'); // false
```

## 3. endsWith()

Checks whether the string ends with the specified characters:

```dart
str3.endsWith('ipsum'); // true
str3.endsWith('oopsum'); // false
```

## 4. toLowerCase(), toUpperCase()

Converts the string to lowercase and uppercase formats:

```dart
str1.toLowerCase(); // lorem
str1.toUpperCase(); // LOREM
```

## 5. split()

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

```dart
str3.split('\n'); // ['Multi', 'Line', 'Lorem Lorem ipsum'];
```

## 6. splitMapJoin()

Splits the string, converts each list item, and combines them into a new string:

```dart
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:

```dart
str3.indexOf('rem'); // 13
str3.lastIndexOf('rem'); // 19
``` 

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

```dart
str3.lastIndexOf('rem', 18); // 13
```

## 8. trim()

Removes leading and trailing whitespaces:

```dart
"   $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:

```dart
str1.padLeft(8, 'x'); // xxLorem
str1.padRight(8, 'x'); // Loremxx
```

## 10. replaceAll()

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

```dart
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](https://dev.to/graphicbeacon/learn-dart-before-you-flutter-2997) to grasp the basics. The code snippets for this article are available on [DartPad](https://dartpad.dartlang.org/abea509cc44de22b5c714c686b17c2ce).

**Like and follow me** 😍 for more articles on Dart. Thanks so much.

## Quicklinks

1. [String class Documentation](https://api.dartlang.org/stable/2.0.0/dart-core/String-class.html)
2. **[Free Dart screencasts on Egghead.io](https://egghead.io/instructors/jermaine-oppong)**

---

Continue reading:

{% link https://dev.to/graphicbeacon/top-7-date-methods-you-should-know-dart-2ap0 %}

---