Dart – Check if String Contains other String

To check if a string contains other string in Dart, call contains() method on this string and pass the other string as argument.

contains() method returns returns a boolean value of true if this string contains the other string, or false if the this string does not contain other string.

Syntax

The syntax to call contains() method on this string object str and pass the other string other as argument is

str.contains(other)

Example

In this example, we take two strings and check if the first string contains the other string.

main.dart

void main() {
  var str = 'Hello World?';
  var other = 'ello';
  if (str.contains(other)) {
    print('This string contains other string.');
  } else {
    print('This string does not contain other string.');
  }
}

Output

This string contains other string.

Conclusion

In this Dart Tutorial, we learned how to check if a string contains other string, using contains() method of String class, with examples.