Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:core
  3. String class
String
description

String classabstractfinal

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 letters

A 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

Implemented types
Available extensions

Constructors

String.fromCharCode(intcharCode)
Allocates a new string containing the specifiedcharCode.
factory
String.fromCharCodes(Iterable<int>charCodes, [intstart =0,int?end])
Allocates a new string containing the specifiedcharCodes.
factory
String.fromEnvironment(Stringname, {StringdefaultValue =""})
Value forname in the compilation configuration environment declaration.
const
factory

Properties

codeUnitsList<int>
An unmodifiable list of the UTF-16 code units of this string.
no setter
hashCodeint
A hash code derived from the code units of the string.
no setteroverride
isEmptybool
Whether this string is empty.
no setter
isNotEmptybool
Whether this string is not empty.
no setter
lengthint
The length of the string.
no setter
runesRunes
AnIterable of Unicode code-points of this string.
no setter
runtimeTypeType
A representation of the runtime type of the object.
no setterinherited
toJSJSString

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 givenindex.
compareTo(Stringother)int
Compares this string toother.
override
contains(Patternother, [intstartIndex =0])bool
Whether this string contains a match ofother.
endsWith(Stringother)bool
Whether this string ends withother.
indexOf(Patternpattern, [intstart =0])int
Returns the position of the first match ofpattern in this string,starting atstart, inclusive:
lastIndexOf(Patternpattern, [int?start])int
The starting position of the last matchpattern in this string.
matchAsPrefix(Stringstring, [intstart =0])Match?
Matches this pattern against the start ofstring.
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 thanwidth.
padRight(intwidth, [Stringpadding =' '])String
Pads this string on the right if it is shorter thanwidth.
replaceAll(Patternfrom,Stringreplace)String
Replaces all substrings that matchfrom withreplace.
replaceAllMapped(Patternfrom,Stringreplace(Matchmatch))String
Replace all substrings that matchfrom by a computed string.
replaceFirst(Patternfrom,Stringto, [intstartIndex =0])String
Creates a new string with the first occurrence offrom replaced byto.
replaceFirstMapped(Patternfrom,Stringreplace(Matchmatch), [intstartIndex =0])String
Replace the first occurrence offrom in this string.
replaceRange(intstart,int?end,Stringreplacement)String
Replaces the substring fromstart toend withreplacement.
split(Patternpattern)List<String>
Splits the string at matches ofpattern and 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 ofpattern.
substring(intstart, [int?end])String
The substring of this string fromstart, 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 withother.
operator ==(Objectother)bool
Whetherother is aString with the same sequence of code units.
override
operator [](intindex)String
The character (as a single-code-unitString) at the givenindex.
  1. Dart
  2. dart:core
  3. String class
dart:core library

[8]ページ先頭

©2009-2025 Movatter.jp