A sequence of UTF-16 code units.
Strings are mainly used to represent text. A character may be represented bymultiple code points, each code point consisting of one or two codeunits. For example, the Papua New Guinea flag character requires four codeunits to represent two code points, but should be treated like a singlecharacter: "🇵🇬". Platforms that do not support the flag character may showthe letters "PG" instead. If the code points are swapped, it instead becomesthe Guadeloupe flag "🇬🇵" ("GP").
A string can be either single or multiline. Single line strings arewritten using matching single or double quotes, and multiline strings arewritten using triple quotes. The following are all valid Dart strings:
'Single quotes';"Double quotes";'Double quotes in "single" quotes';"Single quotes in 'double' quotes";'''Amultilinestring''';"""Anothermultilinestring""";Strings are immutable. Although you cannot change a string, you can performan operation on a string which creates a new string:
const string = 'Dart is fun';print(string.substring(0, 4)); // 'Dart'You can use the plus (+) operator to concatenate strings:
const string = 'Dart ' + 'is ' + 'fun!';print(string); // 'Dart is fun!'Adjacent string literals are concatenated automatically:
const string = 'Dart ' 'is ' 'fun!';print(string); // 'Dart is fun!'You can use${} to interpolate the value of Dart expressionswithin strings. The curly braces can be omitted when evaluating identifiers:
const string = 'dartlang';print('$string has ${string.length} letters'); // dartlang has 8 lettersA string is represented by a sequence of Unicode UTF-16 code unitsaccessible through thecodeUnitAt or thecodeUnits members:
const string = 'Dart';final firstCodeUnit = string.codeUnitAt(0);print(firstCodeUnit); // 68, aka U+0044, the code point for 'D'.final allCodeUnits = string.codeUnits;print(allCodeUnits); // [68, 97, 114, 116]A string representation of the individual code units is accessible throughthe index operator:
const string = 'Dart';final charAtIndex = string[0];print(charAtIndex); // 'D'The characters of a string are encoded in UTF-16. Decoding UTF-16, whichcombines surrogate pairs, yields Unicode code points. Following a similarterminology to Go, Dart uses the name 'rune' for an integer representing aUnicode code point. Use therunes property to get the runes of a string:
const string = 'Dart';final runes = string.runes.toList();print(runes); // [68, 97, 114, 116]For a character outside the Basic Multilingual Plane (plane 0) that iscomposed of a surrogate pair,runes combines the pair and returns asingle integer. For example, the Unicode character for amusical G-clef ('𝄞') with rune value 0x1D11E consists of a UTF-16 surrogatepair:0xD834 and0xDD1E. UsingcodeUnits returns the surrogate pair,and usingrunes returns their combined value:
const clef = '\u{1D11E}';for (final item in clef.codeUnits) { print(item.toRadixString(16)); // d834 // dd1e}for (final item in clef.runes) { print(item.toRadixString(16)); // 1d11e}TheString class cannot be extended or implemented. Attempting to do soyields a compile-time error.
Other resources
- StringBuffer to efficiently build a string incrementally.
- RegExp to work with regular expressions.
- Strings and regular expressions
- Implemented types
- Available extensions
Constructors
- String.fromCharCode(intcharCode)
- Allocates a new string containing the specified
charCode.factory - String.fromCharCodes(Iterable<
int> charCodes, [intstart =0,int?end]) - Allocates a new string containing the specified
charCodes.factory - String.fromEnvironment(Stringname, {StringdefaultValue =""})
- Value for
namein the compilation configuration environment declaration.constfactory
Properties
- codeUnits→List<
int> - An unmodifiable list of the UTF-16 code units of this string.no setter
- hashCode→int
- A hash code derived from the code units of the string.no setteroverride
- isEmpty→bool
- Whether this string is empty.no setter
- isNotEmpty→bool
- Whether this string is not empty.no setter
- length→int
- The length of the string.no setter
- runes→Runes
- AnIterable of Unicode code-points of this string.no setter
- runtimeType→Type
- A representation of the runtime type of the object.no setterinherited
- toJS→JSString
Available onString, provided by theStringToJSString extension
Converts thisString to aJSString.no setter
Methods
- allMatches(
Stringstring, [intstart =0])→Iterable< Match> - Matches this pattern against the string repeatedly.inherited
- codeUnitAt(
intindex)→int - Returns the 16-bit UTF-16 code unit at the given
index. - compareTo(
Stringother)→int - Compares this string to
other.override - contains(
Patternother, [intstartIndex =0])→bool - Whether this string contains a match of
other. - endsWith(
Stringother)→bool - Whether this string ends with
other. - indexOf(
Patternpattern, [intstart =0])→int - Returns the position of the first match of
patternin this string,starting atstart, inclusive: - lastIndexOf(
Patternpattern, [int?start])→int - The starting position of the last match
patternin this string. - matchAsPrefix(
Stringstring, [intstart =0])→Match? - Matches this pattern against the start of
string.inherited - noSuchMethod(
Invocationinvocation)→ dynamic - Invoked when a nonexistent method or property is accessed.inherited
- padLeft(
intwidth, [Stringpadding =' '])→String - Pads this string on the left if it is shorter than
width. - padRight(
intwidth, [Stringpadding =' '])→String - Pads this string on the right if it is shorter than
width. - replaceAll(
Patternfrom,Stringreplace)→String - Replaces all substrings that match
fromwithreplace. - replaceAllMapped(
Patternfrom,Stringreplace(Matchmatch))→String - Replace all substrings that match
fromby a computed string. - replaceFirst(
Patternfrom,Stringto, [intstartIndex =0])→String - Creates a new string with the first occurrence of
fromreplaced byto. - replaceFirstMapped(
Patternfrom,Stringreplace(Matchmatch), [intstartIndex =0])→String - Replace the first occurrence of
fromin this string. - replaceRange(
intstart,int?end,Stringreplacement)→String - Replaces the substring from
starttoendwithreplacement. - split(
Patternpattern)→List< String> - Splits the string at matches of
patternand returns a list of substrings. - splitMapJoin(
Patternpattern, {StringonMatch(Match)?,StringonNonMatch(String)?})→String - Splits the string, converts its parts, and combines them into a newstring.
- startsWith(
Patternpattern, [intindex =0])→bool - Whether this string starts with a match of
pattern. - substring(
intstart, [int?end])→String - The substring of this string from
start, inclusive, toend, exclusive. - toLowerCase(
)→String - Converts all characters in this string to lower case.
- toString(
)→String - A string representation of this object.inherited
- toUpperCase(
)→String - Converts all characters in this string to upper case.
- trim(
)→String - The string without any leading and trailing whitespace.
- trimLeft(
)→String - The string without any leading whitespace.
- trimRight(
)→String - The string without any trailing whitespace.
Operators
- operator *(
inttimes)→String - Creates a new string by concatenating this string with itself a numberof times.
- operator +(
Stringother)→String - Creates a new string by concatenating this string with
other. - operator ==(
Objectother)→bool - Whether
otheris aStringwith the same sequence of code units.override - operator [](
intindex)→String - The character (as a single-code-unitString) at the given
index.