String in Dart Programming


A String in dart is a sequence or series of characters. The characters can be - special characters, numbers or letters. In Dart, we can represent strings with both the single quotes and double quotes.

Example

'a string value' or "another string value"

Both the examples are valid examples of a string in Dart.

In Dart, we can declare strings with the help of the String keyword and also we can use the var keyword as well.

Example

Consider the example shown below −

 Live Demo

void main(){
   String name = "Tuts";
   var herName = "Shreya";
   print(name);
   print(herName);
}

In the above example, we have declared two strings and both these are valid strings in Dart. It should be noted that one string is declared with the help of the String keyword and the other string is declared with the help of the var keyword.

Output

Tuts
Shreya

Note − To print a string in dart, we can simply use the print() function. It prints the formatted message to the terminal, we can pass a string as a message or we can pass a variable as well, both works fine.

Example

Consider the example shown below −

 Live Demo

void main(){
   String name = "Tuts";
   print(name);
   print("TutorialsPoint");
}

In the above example, we are passing the string to the print() function directly in the second print statement, and in the first case, we simply pass a variable to it, which contains a string value.

Output

Tuts
TutorialsPoint

String Concatenation in Dart

String concatenation is the process of adding two strings in Dart. It is usually done with the help of the + operator symbol.

Syntax

str1 + str2;

Example

Let's take an example of string concatenation in Dart, where we add two string values.

Consider the example shown below −

 Live Demo

void main(){
   String name = "Tutorials";
   var anotherName = "Point";
   print(name + anotherName);
}

Output

TutorialsPoint

It should be noted that we can append space in between them using the " ".

Example

Consider the example shown below −

 Live Demo

void main(){
   String name = "Tutorials";
   var anotherName = "Point";
   print(name + " " + anotherName);
}

Output

Tutorials Point

Updated on: 24-May-2021

207 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements