Dart API Referencedart:coreString

String interface

The String class represents character strings. Strings are immutable. A string is represented by a list of 32-bit Unicode scalar character codes accessible through the charCodeAt or the charCodes method.

interface String
    extends Comparable, Pattern, Sequence<String>
    default StringImplementation {
  /**
   * Allocates a new String for the specified [charCodes].
   */
  String.fromCharCodes(List<int> charCodes);

  /**
   * Gets the character (as [String]) at the given [index].
   */
  String operator [](int index);

  /**
   * Gets the scalar character code at the given [index].
   */
  int charCodeAt(int index);

  /**
   * The length of the string.
   */
  int get length;

  /**
   * Returns whether the two strings are equal. This method compares
   * each individual scalar character codes of the strings.
   */
  bool operator ==(String other);

  /**
   * Returns whether this string ends with [other].
   */
  bool endsWith(String other);

  /**
   * Returns whether this string starts with [other].
   */
  bool startsWith(String other);

  /**
   * Returns the first location of [other] in this string starting at
   * [start] (inclusive).
   * Returns -1 if [other] could not be found.
   */
  int indexOf(String other, [int start]);

  /**
   * Returns the last location of [other] in this string, searching
   * backward starting at [start] (inclusive).
   * Returns -1 if [other] could not be found.
   */
  int lastIndexOf(String other, [int start]);

  /**
   * Returns whether this string is empty.
   */
  bool isEmpty();

  /**
   * Creates a new string by concatenating this string with [other].
   */
  String concat(String other);

  /**
   * Returns a substring of this string in the given range.
   * [startIndex] is inclusive and [endIndex] is exclusive.
   */
  String substring(int startIndex, [int endIndex]);

  /**
   * Removes leading and trailing whitespace from a string. If the
   * string contains leading or trailing whitespace a new string with
   * no leading and no trailing whitespace is returned. Otherwise, the
   * string itself is returned.
   */
  String trim();

  /**
   * Returns whether this string contains [other] starting
   * at [startIndex] (inclusive).
   */
  bool contains(Pattern other, [int startIndex]);

  /**
   * Returns a new string where the first occurence of [from] in this string
   * is replaced with [to].
   */
  String replaceFirst(Pattern from, String to);

  /**
   * Returns a new string where all occurences of [from] in this string
   * are replaced with [to].
   */
  String replaceAll(Pattern from, String to);

  /**
   * Splits the string around matches of [pattern]. Returns
   * a list of substrings.
   */
  List<String> split(Pattern pattern);

  /**
   * Returns a list of the characters of this string.
   */
  List<String> splitChars();

  /**
   * Returns a list of the scalar character codes of this string.
   */
  List<int> charCodes();

  /**
   * If this string is not already all lower case, returns a new string
   * where all characters  are made lower case. Returns [:this:] otherwise.
   */
  String toLowerCase();

  /**
   * If this string is not already all uper case, returns a new string
   * where all characters are made upper case. Returns [:this:] otherwise.
   */
  String toUpperCase();
}

Default class

StringImplementation

Extends

Sequence<T>, Pattern, Comparable

Constructors

new String.fromCharCodes(List<int> charCodes) #

Allocates a new String for the specified charCodes.

String.fromCharCodes(List<int> charCodes);

Properties

final int length #

The length of the string.

int get length;

Operators

String operator [](int index) #

Gets the character (as String) at the given index.

String operator [](int index);

bool operator ==(String other) #

Returns whether the two strings are equal. This method compares each individual scalar character codes of the strings.

bool operator ==(String other);

Methods

Iterable<Match> allMatches(String str) #

inherited from Pattern
Iterable<Match> allMatches(String str);

int charCodeAt(int index) #

Gets the scalar character code at the given index.

int charCodeAt(int index);

List<int> charCodes() #

Returns a list of the scalar character codes of this string.

List<int> charCodes();

abstract int compareTo(Comparable other) #

inherited from Comparable

Compares this object to another Comparable

Returns a value like a Comparator when comparing this to other.

May throw an ArgumentError if other is of a type that is not comparable to this.

String concat(String other) #

Creates a new string by concatenating this string with other.

String concat(String other);

bool contains(Pattern other, [int startIndex]) #

Returns whether this string contains other starting at startIndex.

bool contains(Pattern other, [int startIndex]);

bool endsWith(String other) #

Returns whether this string ends with other.

bool endsWith(String other);

int indexOf(String other, [int start]) #

Returns the first location of other in this string starting at start. Returns -1 if other could not be found.

int indexOf(String other, [int start]);

bool isEmpty() #

Returns whether this string is empty.

bool isEmpty();

int lastIndexOf(String other, [int start]) #

Returns the last location of other in this string, searching backward starting at start. Returns -1 if other could not be found.

int lastIndexOf(String other, [int start]);

String replaceAll(Pattern from, String to) #

Returns a new string where all occurences of from in this string are replaced with to.

String replaceAll(Pattern from, String to);

String replaceFirst(Pattern from, String to) #

Returns a new string where the first occurence of from in this string is replaced with to.

String replaceFirst(Pattern from, String to);

List<String> split(Pattern pattern) #

Splits the string around matches of pattern. Returns a list of substrings.

List<String> split(Pattern pattern);

List<String> splitChars() #

Returns a list of the characters of this string.

List<String> splitChars();

bool startsWith(String other) #

Returns whether this string starts with other.

bool startsWith(String other);

String substring(int startIndex, [int endIndex]) #

Returns a substring of this string in the given range. startIndex is inclusive and endIndex is exclusive.

String substring(int startIndex, [int endIndex]);

String toLowerCase() #

If this string is not already all lower case, returns a new string where all characters are made lower case. Returns this otherwise.

String toLowerCase();

String toUpperCase() #

If this string is not already all uper case, returns a new string where all characters are made upper case. Returns this otherwise.

String toUpperCase();

String trim() #

Removes leading and trailing whitespace from a string. If the string contains leading or trailing whitespace a new string with no leading and no trailing whitespace is returned. Otherwise, the string itself is returned.

String trim();