Table of contents
1.
Introduction
2.
Dart Strings
2.1.
Declaration
3.
String Properties
3.1.
isEmpty
3.2.
Length
4.
String Methods
4.1.
substring()
4.2.
toUpperCase()
4.3.
toLowerCase()
4.4.
compareTo()
4.5.
trim()
4.6.
split()
4.7.
Check out this article - String slicing in Python
5.
Frequently Asked Questions
6.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Dart Strings

Author Pradeep Kumar
3 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into the Dart Strings. Dart Strings supports a number of methods, each of which performs a specific operation. We will go through all the methods that are supported by Dart Strings

If you are new to Dart and are yet to set up your environment for the language, you can check out this article.

Dart Strings

A Dart String is a character or UTF-16 code unit sequence. It's used to save text values. The value of the string is enclosed within quotes. 

Declaration

A string can be declared using the String keyword or the var keyword. For example,

void main() {
  String str1 = "Hello World!";
  var str2 = "Welcome to Coding Ninjas!";
  print(str1);
  print(str2);
}

Output:

Hello World!
Welcome to Coding Ninjas!

String Properties

Following are the properties supported by Strings in Dart:

isEmpty

It returns true in case the String is empty. For example,

void main() {
  String str1 = "";
  String str2 = "Not empty";
  print("Is str1 empty? ${str1.isEmpty}");
  print("Is str2 empty? ${str2.isEmpty}");
}

Output:

Is str1 empty? true
Is str2 empty? false

Length

It returns the length of the String. For example,

void main() {
  String str1 = "";
  String str2 = "Not empty";
  print("Length of str1: ${str1.length}");
  print("Length of str2: ${str2.length}");
}

Output:

Length of str1: 0
Length of str2: 9

String Methods

Dart provides a lot of methods to manipulate the Strings, which are as follows:

substring()

This method is used to slice a string. Given a start and an end index, it will return the part of the string that lies between those indices. For example,

void main() {
  String str = "Hello World!";
  print("The Substring of str between 1 and 5 indices is: ${str.substring(1,5)}");
}

Output:

The Substring of str between 1 and 5 indices is: ello

toUpperCase()

It converts all the characters of the given string to upper case. For example,

void main() {
  String str = "Hello World!";
  print("The Upper case Representation of str: ${str.toUpperCase()}");
}

Output:

The Upper case Representation of str: HELLO WORLD!

toLowerCase()

It converts all the characters of the given string to lower case. For example,

void main() {
  String str = "Hello World!";
  print("The Lower case Representation of str: ${str.toLowerCase()}");
}

Output:

The Lower case Representation of str: hello world!

compareTo()

It is used to compare two strings. It returns -1 if str1 is lexicographically smaller than str2, and 1 if str1 is larger than str2. In case both strings are equal, it returns 0. For example,

void main() {
  String str1 = "AA";
  String str2 = "AB";
  String str3 = "AA";
  print("Str1 and Str2 comparison: ${str1.compareTo(str2)}");
  print("Str2 and Str3 comparison: ${str2.compareTo(str3)}");
  print("Str3 and Str1 comparison: ${str3.compareTo(str1)}");
}

Output:

Str1 and Str2 comparison: -1
Str2 and Str3 comparison: 1
Str3 and Str1 comparison: 0

trim()

It trims any trailing and leading spaces of a string. For example,

void main() {
  String str1 = "AA   ";
  String str2 = "AB";
  print("str1, str2: ${str1.trim()}, $str2");
}

Output:

str1, str2: AA, AB

As you can see from the output, the output has no white spaces after str1.

split()

This method is used to split a string by specifying a particular delimiter. For example,

void main() {
  String str1 = "A,B,C,D";
  var l = str1.split(",");
  print(l);
}

Output:

[A, B, C, D]

Check out this article - String slicing in Python

Frequently Asked Questions

  1. Can single quotes be used for strings in Dart?
    Yes, Dart supports both types of quotes for strings. Like python, dart also provides the functionality of using either single or double quotes for strings.
  2. Does dart support multiline strings?
    Yes, Dart supports multiline strings. Multiline strings are enclosed within triple quotes.
  3. Is the end index mandatory for the substring method?
    No. In case the end index is not given to the method, it will return a substring starting from the start index to the end of the entire string.

Conclusion 

In this article, we have extensively discussed the Strings in Dart. We hope that this blog has helped you enhance your knowledge regarding the Dart String, and to learn more about Dart Classes. And to learn in-depth about android development, check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow. Happy Coding!

Recommended problems -

Live masterclass