Flutter string contains:
The “contains” method in Flutter is used to check if a string contains a specific substring. It returns a boolean value indicating whether the substring is found or not.
Here’s an example:
String mainString = "Hello, World!";
String substring = "World";
bool containsSubstring = mainString.contains(substring);
print('Does the main string contain the substring? $containsSubstring');
In this example, we declare a main string “Hello, World!” and a substring “World”. The “contains” method is then used to check if the main string contains the substring. The result is stored in the “containsSubstring” boolean variable.
Finally, we print the result, which would be true in this case since the substring “World” is present in the main string.
Note that the “contains” method is case-sensitive. If you want a case-insensitive search, you can convert both the main string and substring to lowercase or uppercase before performing the check.
Here’s an example with case-insensitive search:
String mainString = "Hello, World!";
String substring = "world";
bool containsSubstring = mainString.toLowerCase().contains(substring.toLowerCase());
print('Does the main string contain the substring? $containsSubstring');
In this modified example, both the main string and substring are converted to lowercase using the “toLowerCase” method before performing the check. As a result, the search becomes case-insensitive, and the output would be true since the lowercase substring “world” is present in the lowercase main string.