Skip to main content

Strings in Dart

String data types help you to store text data. In string, you can represent your name, address, or even the entire complete text of the book. It is used to hold a series or sequence of characters – letters, numbers, and special characters. You can use single or double or triple quotes to represent string.

Example​

String Concatenation​

You can combine one string with another string. This is called concatenation. In Dart, you can use the + operator or use interpolation to concatenate the string. Interpolation makes it easy to read and understand the code.

Example​

Properties Of String​

  • codeUnits: Returns an unmodifiable list of the UTF-16 code units of this string.
  • isEmpty: Returns true if this string is empty.
  • isNotEmpty: Returns false if this string is empty.
  • length: Returns the length of the string including space, tab, and newline characters.

String Properties Example In Dart​

Methods Of String​

  • toLowerCase(): Converts all characters in this string to lowercase.
  • toUpperCase(): Converts all characters in this string to uppercase.
  • trim(): Returns the string without any leading and trailing whitespace.
  • compareTo(): Compares this object to another.
  • replaceAll(): Replaces all substrings that match the specified pattern with a given value.
  • split(): Splits the string at matches of the specified delimiter and returns a list of substrings.
  • toString(): Returns a string representation of this object.
  • substring(): Returns the substring of this string that extends from startIndex, inclusive, to endIndex, exclusive.
  • codeUnitAt(): Returns the 16-bit UTF-16 code unit at the given index.

String Methods Example In Dart​

Here you will see various methods example, that can help your work a lot better and fast.

Converting String To Uppercase and Lowercase​

You can convert your text to lower case using .toLowerCase() and convert to uppercase using .toUpperCase() method.

Trim String In Dart​

Trim is helpful when removing leading and trailing spaces from the text. This trim method will remove all the starting and ending spaces from the text.

Note

In dart, trim() method doesn’t remove spaces in the middle.

Compare String In Dart​

In dart, you can compare two strings. It will give result 0 when two texts are equal, 1 when the first string is greater than the second, and -1 when the first string is smaller than the second.

Replace String In Dart​

You can replace one value with another with the replaceAll(β€œold”, β€œnew”) method in dart. It will replace all the β€œold” words with β€œnew”. Here in this example, this will replace milk with water.

Split String In Dart​

If you want to split String by comma, space, or other text, you can use the dart split method. It will help you to split String to list.

ToString In Dart​

In dart, toString() represents String representation of the value/object.

SubString In Dart​

When you want to get text from any position then you can use substring in dart.

Reverse String In Dart​

If you want to reverse string in dart, you can reverse it by using different solution. One solution is here.