A string is one of the primitive data types in the Dart language. It is used to store a group of characters enclosed in single double or triple quotes.

Single line strings use single(') and double quotes("). Multi-line strings use triple quotes(''').

Dart language provides String to store.

  • normal characters
  • UTF 16 Unicode characters

The above all are reserved keywords that are not used as an identifier for variable functions or class names in Dart language.

How to Declare and assign String variable in Dart?

Variables are declared like a normal variable declaration in Dart. The default value for the String type is null.

String variable_name=stringvalue;

In the above program.

  • Variables are declared using the String type
  • a type of the variable is declared, It tells the string value type to be stored.
  • string values are assigned to the variable on the same line.

variable_name is a valid identifier in the dart programming language.

the string value can be enclosed with single double or triple quotes for initializing value.

Here is a String variable example declaration and assignment

void main() {
  //declare String type variables
  String message = 'Hello World';
  String message1 = "Hello World";
  String message2 = '''Hello World multiline string''';
  String message3 = """Hello World welcome""";

  print(message.runtimeType); // datatype of an variable: String
  print(message);
  print(message1);
  print(message2);
  print(message3);
}

Output:

String
Hello World
Hello World
Hello World multiline string
Hello World welcome

you can print the string variables with a print statement to the console

How to concatenate two strings in Dart?

Java language provides methods and + symbols to append strings. Dart also provides multiple ways to append a string.

  • Using + operator + operator append a string variables
  • String interpolation Syntax : It uses the ${} syntax with string variables enclosed in single or double or triple quotes.
void main() {
  String str1 = 'Hello';
  String str2 = 'World';
  // + operator
  var plusOperatorResult = str1 + str2;
  // string interpolation syntax enclosed in string quotes
  var interpolationResult1 = '$str1$str2';
  var interpolationResult2 = "$str1$str2";
  var interpolationResult3 = '''$str1$str2''';

  print(plusOperatorResult);
  print(interpolationResult1);
  print(interpolationResult2);

  print(interpolationResult3);
}

Output:

HelloWorld
HelloWorld
HelloWorld
HelloWorld

String Unicode character

Strig not only contains alphabets, But it also stores Unicode characters such as Emoji.

Here is a sample example string Unicode characters

void main() {
  String str1 = '🔥';
  String str2 = "\ue934";

  print(str1);
  print(str2);
}

Output:

🔥
