SoFunction
Updated on 2025-04-12

Summary of common methods of Dart String strings

Some common methods of strings in Flutter

1 length: Returns the length of the string.

length is a method that returns an integer type to get the length of a string, for example:

String str = "Hello World!";
int length = ;
print(length); // Output: 12

In this example, the value of length is 12 because the string has 12 characters.

2 isEmpty: determines whether the string is empty.

isEmpty is a method that returns a Boolean value, used to determine whether a string is empty, for example:

String str = "";
bool isEmpty = ;
print(isEmpty); // Output: true

In this example, the value of isEmpty is true because the string is empty. If the string is not empty, the value of isEmpty is false.

isNotEmpty is a method that returns a boolean value. In contrast to isEmpty, it is used to determine whether a string is not empty, for example:

String str = "Hello";
bool isNotEmpty = ;
print(isNotEmpty); // Output: true

In this example, the value of isNotEmpty is true because the string is not empty. If the string is empty, the value of isNotEmpty is false.

3 toUpperCase(): Convert all lowercase letters in the string to uppercase letters.

toUpperCase() is a method that converts all lowercase letters in a string to uppercase letters, for example:

String str = "Hello World!";
String upperCaseStr = ();
print(upperCaseStr); // Output: HELLO WORLD!

In this example, the toUpperCase() method converts all lowercase letters in the original string to uppercase letters, returning a new string HELLO WORLD! . The value of the original string str has not changed.

4 toLowerCase(): Convert all uppercase letters in the string to lowercase letters.

Used to convert all uppercase letters in a string to lowercase letters, for example:

String str = "Hello World!";
String lowerCaseStr = ();
print(lowerCaseStr); // Output: hello world!

In this example, the toLowerCase() method converts all uppercase letters in the original string to lowercase letters, returning a new string hello world! . The value of the original string str has not changed.

5 startsWith(pattern): determines whether the string starts with the specified pattern (pattern).

Used to determine whether a string starts with the specified pattern and returns a Boolean value. For example:

String str = "Hello World!";
bool startsWithHello = ("Hello");
print(startsWithHello); // Output: true

In this example, the startsWith("Hello") method determines whether the string str starts with "Hello" and returns a boolean value true.

6 endsWith(pattern): determines whether the string ends in the specified pattern.

Used to determine whether a string ends in the specified pattern and returns a boolean value. For example:

String str = "Hello World!";
bool endsWithWorld = ("World!");
print(endsWithWorld); // Output: true

In this example, the endsWith("World!") method determines whether the string str ends with "World!" and returns a boolean value true.

7 contains(pattern): determines whether the string contains the specified pattern.

Returns a Boolean value to determine whether a string contains the specified pattern, for example:

String str = "Hello World!";
bool containsHello = ("Hello");
print(containsHello); // Output: true

In this example, the contains("Hello") method determines whether the string str contains the "Hello" pattern, returning a boolean value true.

8 replaceFirst(oldValue, newValue): Replace the first oldValue in the string with newValue.

Used to replace the first matching oldValue in the original string with newValue , returning a new string. For example:

String str = "Hello World!";
String newStr = ("World", "Dart");
print(newStr); // Output: Hello Dart!

In this example, the replaceFirst("World", "Dart") method replaces the first matching "World" in the original string with "Dart" and returns a new string "Hello Dart!". The value of the original string str has not changed.

9 replaceAll(oldValue, newValue): Replace all oldValues ​​that appear in the string with newValue.

Used to replace all the matching oldValue parts in a string with newValue and return a new string. For example:

String str = "Hello World!";
String newStr = ("o", "e");
print(newStr); // Output: Helle Werld!

In this example, the replaceAll("o", "e") method replaces all the parts of the original string that match "o" with "e" and returns a new string "Helle Werld!". The value of the original string str has not changed.

10 substring(startIndex, endIndex): Intercept part of the string, start from startIndex to endIndex.

Used to get a substring within a specified range in a string and return a new string. where startIndex represents the start position of the substring (including the characters at that position), and endIndex represents the end position of the substring (excluding the characters at that position). For example:

String str = "Hello World!";
String subStr = (0, 5);
print(subStr); // Output: Hello

In this example, the substring(0, 5) method gets the substring in the string str starting from position 0 to position 5 (excluding position 5), and returns a new string "Hello" . The value of the original string str has not changed. Note that the substring method includes the start position and does not include the end position. If the endIndex parameter is omitted, the method will intercept from startIndex to the end of the string.

11 split(separator): Split the string into an array of strings according to the specified separator.

Split a string into multiple substrings according to the specified separator, returning a list of strings. For example:

String str = "Hello World!";
List<String> strList = (" ");
print(strList); // Output: [Hello, World!]

In this example, the split(" ") method separates the string str into two substrings "Hello" and "World!" according to spaces, forming a string list [Hello, World!] to return. Note that the split() method can only accept one parameter, i.e. the delimiter. If the separator parameter is omitted, it is split by default by space or tab character ( \t ).

12 trim(): Remove the spaces at the beginning and end of the string.

Used to remove spaces (or other whitespace characters) at both ends of a string and return a new string. For example:

String str = "  Hello World!  ";
String newStr = ();
print(newStr); // Output: Hello World!

In this example, the trim() method removes the spaces at both ends of the string str and returns a new string "Hello World!". The value of the original string str has not changed.

13 compareTo(otherString)

Compare the sizes of two strings and return an integer value. If the current string is smaller than otherString, it returns a negative number. If the current string is larger than otherString, it returns a positive number. If the current string and otherString are equal, it returns 0. For example:

String str1 = "Hello";
String str2 = "World";
int result = (str2);
print(result); // Output: -15

In this example, the compareTo(str2) method compares the dictionary order of the strings str1 and str2, and since the string str1 is smaller than the string str2 , it returns a negative number -15 .

14 codeUnitAt

Unicode encoding for characters at the specified position in a string, returning an integer. where index indicates the position of the character to be retrieved, starting from 0. For example:

String str = "Hello";
int codeUnit = (1);
print(codeUnit); // Output: 101, the Unicode encoding corresponding to the character 'e'.

In this example, the codeUnitAt(1) method gets the Unicode encoding of the character with position 1 (that is, the second character) in the string str, that is, the Unicode encoding 101 of the character 'e' (according to the ASCII encoding table).

15 indexOf

Used to find whether a string contains the specified substring. If included, return the location where the substring first appeared in the original string (counted from left to right, starting from 0). If not included, return -1. Where substring represents the substring to be found, startIndex represents the starting position of the search (counted from left to right, starting from 0), which can be omitted, and the default is 0. For example:

String str = "Hello World!";
int index = ("o");
print(index); // Output: 4

In this example, the indexOf("o") method looks for the first occurrence of substring "o" in the string str, and returns its position 4 after finding it. Note that this method returns the position of the substring in the original string, not the length of the substring. If you want to find multiple substrings, you can specify the location where the last lookup ends in the startIndex parameter. If the substring does not exist, return -1.

16 lastIndexOf

Used to find whether a string contains the specified substring. If included, return the last occurrence of the substring in the original string (counted from right to left, starting from 0). If not included, return -1. Where substring represents the substring to be found, and startIndex represents the starting position of the search (counted from right to left, starting from 0), which can be omitted, and defaults to the end of the string. For example:

String str = "Hello World!";
int index = ("o");
print(index); // Output: 7

In this example, the lastIndexOf("o") method looks for the last occurrence of substring "o" in the string str, and returns its position 7 after finding it. Note that this method returns the position of the substring in the original string, not the length of the substring. If you want to find multiple substrings, you can specify the location where the last lookup ends in the startIndex parameter. If the substring does not exist, return -1.

17 padLeft

padLeft(width, [padding]) is a method that fills a specified number of characters to the left of a string. Where, width represents the total length of the string after filling, padding represents the characters used for filling, which can be omitted, and defaults to space characters. For example:

String str = "Hello";
String newStr = (10, "-");
print(newStr); // Output: -----Hello

In this example, the padLeft(10, "-") method fills the left side of the string str with 5 "-" characters until the length reaches 10, returning a new string "----Hello". If the length of the original string is greater than or equal to the specified length, no characters will be filled and the original string will be returned directly.

18 padRight

padRight(width, [padding]) is a method that fills a specified number of characters to the right of a string. Where, width represents the total length of the string after filling, padding represents the characters used for filling, which can be omitted, and defaults to space characters. For example:

String str = "Hello";
String newStr = (10, "-");
print(newStr); // Output: Hello-----

In this example, the padRight(10, "-") method fills the right side of the string str with 5 "-" characters until the length reaches 10, returning a new string "Hello-----" . If the length of the original string is greater than or equal to the specified length, no characters will be filled and the original string will be returned directly.

19 replaceFirstMapped

replaceFirstMapped(from, Function matchFunction) is a method used to replace the first qualified substring in a string based on regular expression matching. where from represents the regular expression of the substring to be replaced, and matchFunction represents the replacement logic after matching the substring. For example:

String str = "Hello World!";
String newStr = (RegExp(r"(\w+) (\w+)"), (match) {
  return "${(2)} ${(1)}";
});
print(newStr); // Output: World! Hello

In this example, replaceFirstMapped(RegExp(r"(\w+) (\w+)")) method matches a "Hello World"-like substring that appears first in the string str. After matching, execute the logic in the matching function (match) {...}, exchange two words in the substring and return a new string "World! Hello".

Note that the match parameter in the matching function is Match type, indicating the matched substring and its grouping information. The contents in the nth group can be obtained through the (n) method. If you want to replace all matching substrings, you can use the replaceAllMapped() method.

20 replaceRange

replaceRange(start, end, replacement) is a method that replaces a substring within a specified range in a string. where start represents the start position of the substring to be replaced (counted from left to right, starting from 0), end represents the end position of the substring to be replaced (counted from left to right, starting from 0, not containing the characters at that position), and replacement represents the replaced string. For example:

String str = "Hello World!";
String newStr = (0, 5, "Hi");
print(newStr); // Output: Hi World!

In this example, the replaceRange(0, 5, "Hi") method replaces the substring "Hello" in the string str from position 0 to position 5 (excluding 5) with "Hi" and returns a new string "Hi World!".

Note that the replacement range includes the start position and the end position. If you want to replace it to the end of the string, you can omit or assign the end parameter to the string length.

21 splitMapJoin

splitMapJoin(pattern, {onMatch, onNonMatch}) is a method that can split a string into several substrings according to the specified pattern, and process each substring in the specified way and merge it into a new string. where pattern represents the pattern used to match substrings (can be a string, a regular expression, or a function), onMatch represents the function that handles the matching substrings, onNonMatch represents the function that handles the non-match substrings, and if not specified, the default processing function is used. For example:

String str = "Hello World!";
String newStr = ("o", onMatch: (match) => "*", onNonMatch: (nonMatch) => ());
print(newStr); // Output: HEL* W*RLD!

In this example, the splitMapJoin("o", onMatch: (match) => "*", onNonMatch: (nonMatch) => ()) method divides the string str into several substrings according to the "o" character. The matching substring will be replaced with "*" and the non-match substring will be converted into capital letters. Finally, all substrings are merged into a new string "HEL* W*RLD!" .

Note that the onMatch and onNonMatch parameters can be functions, the parameters of the function are the substring content, and the return value of the function is the processed substring content, or it can be a string, indicating that the substring content is directly replaced. If you use regular expressions as a pattern, you can use the RegExp class to create a regular expression object.

22 runes

A string in Dart is a sequence of Unicode characters, and each Unicode character corresponds to one or more UTF-16-encoded code units. Use the runes attribute to get the Unicode character sequence in the string and return an iterable Unicode character sequence. For example:

String str = "🍎 Hello Dart!";
Iterable<int> runes = ;
for (int rune in runes) {
  print(rune);
}

In this example, the string str contains a Unicode character 🍎 , which uses the runes attribute to get the Unicode character sequence of the string, and iterates over and outputs the encoded value of each character.

Note that using the runes attribute returns a Unicode character sequence, each character corresponds to a value of type int, rather than a UTF-16-encoded code unit sequence. If you want to convert a Unicode character sequence into a string, you can use the () method.

23

fromCharCodes() is a static method used to convert one or more Unicode character sequences into a string. Where, the Unicode character sequence can be a List or an iterable object, and each Unicode character corresponds to a numeric value of type int. For example:

List<int> codes = [72, 101, 108, 108, 111]; // Unicode encoding of "Hello"String str = (codes);
print(str); // Output: Hello

In this example, a Unicode encoded list [72, 101, 108, 108, 111] containing the string "Hello" is converted to a string.

Note that when using the fromCharCodes() method, it is necessary to ensure that the encoded value corresponding to each Unicode character is valid. If the encoded value is invalid, the method throws an exception.

The above is an overview of common methods for Dart String strings. I have compiled the detailed content of about 4,000 words. For more information about Dart String string methods, please pay attention to my other related articles!