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)

Examples

Check if a string is present in another string

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

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.