The contains()
method is to check whether a string contains another string. Returns true if this string contains a match of other
. Otherwise returns false.
The following code snippet shows does this string contains a given string.
String message = 'Welcome to dart Programming language';
message.contains('dart'); // true
If startIndex
is provided, this method matches only at OR
after that index.
message.contains('dart', 10); // true
The startsWith()
method is to check whether a string start with another string. Returns true if this string starts with a match of pattern
. Otherwise returns false.
The following code snippet shows, does this string starts with given string.
String message = 'Dart Programming language';
message.startsWith('Dart'); // true
The endsWith()
method is to check whether a string end with another string. Returns true if this string ends with [other]. Otherwise return false.
The following code snippet show, does this string ends with given string.
String message = 'Dart Programming language';
message.endsWith('language'); // true
The indexOf()
method is to return the position of the first match of pattern
from this string.
The following code snippet shows, from this string get the index of given string.
String message = 'Dart Programming language';
message.indexOf('P'); // 5
The substring()
is to return the substring from this string that extends from startIndex
to endIndex
.
The following code snippet shows, from this string how to extract sub string by providing start and end indexes.
String message = 'Welcome to dart Programming language';
print(message.substring(11, 15)); // dart
The split()
method is to split a string using string pattern
and return a list of substrings.
The following code snippet shows how to split this string with space pattern.
main() {
String message = 'Welcome to dart Programming language';
var result = message.split(' ');
print(result.runtimeType); // List<String>
print(result); // [Welcome, to, dart, Programming, language]
}
Use split() with an empty string parameter to get a list of all characters (as Strings).
The following code snippet shows how to get list of characters from this string.
main() {
String skill = 'dart';
var result = skill.split('');
print(result.runtimeType); // List<String>
print(result); // [d, a, r, t]
for (var char in result) {
print(char);
}
}
The index operator []
is to get the character (as a single-code-unit [String]) at the given index.
The following code snippet shows how to get 0th and 11th indexed character from this string.
main() {
String message = 'Welcome to dart Programming language';
print(message[0]); // W
print(message[11]); // d
}
The codeUnits
property is to return an unmodifiable list of the UTF-16 code units of this string.
The following code snippet shows how to get list of UTF-16 code units from this string.
main() {
String message = 'Welcome to dart Programming language';
var codeUnitList = message.codeUnits.toList();
print(codeUnitList[0] == 87); // true
}
The toUpperCase()
method is to convert strings to their uppercase variants.
The following code snippet converts this string to upper case.
main() {
String skill = 'dart';
print(skill.toUpperCase()); // DART
}
The toLowerCase()
method is to convert strings to their lowercase variants.
The following code snippet converts this string to lower case.
main() {
String skill = 'DART';
print(skill.toLowerCase()); // dart
}
The trim()
method is to remove all leading and trailing white space from this string.
The following code shows how to trim this string.
main() {
String skill = ' dart ';
print(skill.trim() == 'dart'); // true
}
The isEmpty
property is to check whether a string is empty.
The following code snippet shows how to check for empty string.
main() {
String skill = '';
if (skill.isEmpty) {
print('Its empty string');
}
}
The isNotEmpty
is to check strings with only white space and not empty.
The following code snippet shows how to check for not empty string.
main() {
String skill = 'Dart lang';
if (skill.isNotEmpty) {
print('This string is not empty');
}
}
Strings are immutable objects, which means you can create them but you can’t change them.
The replaceAll()
method is to replace given substring from this string, and returns a new string without changing the original String.
The following code snippet shows how to replace substring from this string.
main() {
String message = 'Welcome to skillName Programming language';
var result = message.replaceAll(RegExp('skillName'), 'dart');
print(result); // Welcome to dart Programming language
}