Dart Basic- String

Home /

Table of Contents

String in Dart

A Dart string is a sequence of UTF 16 code units. String values in Dart can be represented using either single or double or triple quotes. Single line strings are represented using single or double quotes. Triple quotes are used to represent multi-line strings. 

String  var_name = 'value'  

Strings are immutable. However, strings can be subjected to various operations and the resultant string can be stored as a new value.

The process of creating a new string by appending a value to a static string is termed as concatenation or interpolation. In other words, it is the process of adding a string to another string.

The operator plus (+) is a commonly used mechanism to concatenate / interpolate strings.

void main() {
   String str1 = "hello";
   String str2 = "world";
   String res = str1+str2;
   
   print("The concatenated string : ${res}");
}

It will produce the following output −

The concatenated string : Helloworld

String Properties

NoProperty & Description
1codeUnits
Returns an unmodifiable list of the UTF-16 code units of this string.
2isEmpty
Returns true if this string is empty.
3Length
Returns the length of the string including space, tab and newline characters.

Example:

void main() {
  String text = "Hello, World!";
  
  // Using codeUnits method
  List<int> codeUnitsList = text.codeUnits;
  print("Code units of $text: $codeUnitsList");
  
  // Using isEmpty method
  bool emptyCheck = text.isEmpty;
  print("Is $text empty? $emptyCheck");
  
  // Using length method
  int textLength = text.length;
  print("Length of $text is $textLength");
}

Output:

Code units of Hello, World!: [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
Is Hello, World! empty? false
Length of Hello, World! is 13

String Methods

The String class in the dart core library also provides methods to manipulate strings. Some of these methods are given below −

NoMethods & Description
1toLowerCase()
Converts all characters in this string to lowercase.
2toUpperCase()
Converts all characters in this string to uppercase.
3trim()
Returns the string without any leading and trailing whitespace.
4compareTo()
Compare this object to another.
5replaceAll()
Replaces all substrings that match the specified pattern with a given value.
6split()
Splits the string at matches of the specified delimiter and returns a list of substrings.
7substring()
Returns the substring of this string that extends from startIndex, inclusive, to endIndex, exclusive.
8toString()
Returns a string representation of this object.
9codeUnitAt()
Returns the 16-bit UTF-16 code unit at the given index.

Example:

void main() {
  String str = "   Hello, World!   ";
  
  // Convert to lowercase
  print("Lowercase: ${str.toLowerCase()}");
  
  // Convert to uppercase
  print("Uppercase: ${str.toUpperCase()}");
  
  // Trim whitespace
  print("Trimmed: ${str.trim()}");
  
  // Compare to another string
  print("Compare: ${str.compareTo("   HELLO, WORLD!   ")}");
  
  // Replace all occurrences of a substring
  print("Replaced: ${str.replaceAll("World", "Dart")}");
  
  // Split into a list of substrings
  print("Split: ${str.split(",")}");
  
  // Get a substring
  print("Substring: ${str.substring(3, 8)}");
  
  // Convert to a string
  Object obj = 42;
  print("Object as string: ${obj.toString()}");
}

Output:

Lowercase:    hello, world!   
Uppercase:    HELLO, WORLD!   
Trimmed: Hello, World!
Compare: 1
Replaced:    Hello, Dart!   
Split: [   Hello,  World!   ]
Substring: Hello
Object as string: 42
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article