Open In App

Strings in Dart

Improve
Improve
Like Article
Like
Save
Share
Report

A Dart string is a sequence of UTF-16 code units. With the same rule as that of Python, you can use either single or double quotes to create a string. The string starts with the datatype Var

var string = "I love GeeksforGeeks";
var string1 = 'GeeksforGeeks is a great platform for upgrading skills';

Both the strings above when running on a Dart editor will work perfectly.

You can put the value of an expression inside a string by using ${expression}. It will help the strings to concatenate very easily. If the expression is an identifier, you can skip the {}

Dart




void main () {
var string = 'I do coding';
var string1 = '$string on Geeks for Geeks';
print (string1);
}


 Output :

I do coding on Geeks for Geeks.

Dart also allows us to concatenate the string by + operator as well as we can just separate the two strings by Quotes. The concatenation also works over line breaks which is itself a very useful feature.

Dart




var string = 'Geeks''for''Geeks';
var str = 'Coding is ';
var str1 = 'Fun';
print (string);
print (str + str1);


Output :

GeeksforGeeks
Coding is Fun

We can also check whether two strings are equal by == operator. It compares every element of the first string with every element of the second string.

Dart




void main()
{
    var str = 'Geeks';
    var str1 = 'Geeks';
    if (str == str1) {
        print('True');
    }
}


Output : 

True

Raw strings are useful when you want to define a String that has a lot of special characters. We can create a raw string by prefixing it with r . 

Dart




void main() {
   
  var gfg = r'This is a raw string';
  print(gfg);
}


Output:

This is a raw string


Last Updated : 16 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads