Dart String


Dart String

Dart String is a sequence of the character or UTF-16 code units. It is used to store the text value. The string can be created using single quotes or double-quotes. The multiline string can be created using the triple-quotes. Strings are immutable; it means you cannot modify it after creation.

In Dart, The String keyword can be used to declare the string. The syntax of the string declaration is given below.

Syntax:

  1. String msg = 'Welcome to JavaTpoint';  
  2. or  
  3. String msg1 = "This is double-quoted string example.";  
  4. or  
  5. String msg2 = ' ' ' line1  
  6. line2  
  7. line3'''  

Printing String

The print() function is used to print the string on the screen. The string can be formatted message, any expression, and any other object. Dart provides ${expression}, which is used to put the value inside a string. Let' have at look at the following example.

void main() {   

  1.    String str1 = 'this is an example of a single-line string';   
  2.    String str2 = "this is an example of a double-quotes multiline line string";   
  3.    String str3 = """this is a multiline line   
  4. string using the triple-quotes""";   
  5.   
  6.    var  a = 10;  
  7.    var b = 20;  
  8.    
  9.    print(str1);  
  10.    print(str2);   
  11.    print(str3);   
  12.   
  13.  // We can add expression using the ${expression}.  
  14.    print("The sum is  = ${a+b}");  
  15. }  

Output:

this is an example of a single-line string
this is an example of a double-quotes multiline line string
this is a multiline line
   string using the triple-quotes
The sum is  = 30

String Concatenation

The + or += operator is used to merge the two string. The example is given below.

  1. void main() {   
  2.    String str1 = 'Welcome To ';   
  3.    String str2 = "JavaTpoint";   
  4.    String str3 = str1+str2;  
  5.   
  6.    print(str3);   
  7. }  

Output:

Welcome To JavaTpoint

String Interpolation

The string interpolation is a technique to manipulate the string and create the new string by adding another value. It can be used to evaluate the string including placeholders, variables, and interpolated expression. The ${expression} is used for string interpolation. The expressions are replaced with their corresponding values. Let's understand by the following example.

  1. void main() {   
  2.    String str1 = 'Hello ';   
  3.    String str2 = "World!";   
  4.    String str3 = str1+str2;  
  5.      
  6.  print(str3);   
  7.   
  8.    var x = 26;  
  9.    var y = 10;  
  10.   
  11.    print("The result is  = ${x%y}");  
  12.   
  13.    var name = "Peter";  
  14.    var roll_nu = 101;  
  15.      
  16.    print("My name is ${name}, my roll number is ${roll_nu}");  
  17. }  

Output:

Hello World!
The result is = 6
My name is Peter, my roll number is 101

Explanation -

In the above code, we have declared two strings variable, created a new string after concatenation, and printed the result.

We have created two variables that hold integer value then performed the mod operation and we printed the result using the string interpolation.

We can use the string interpolation as a placeholder, as we have shown in the above example.

String Properties

The Dart provides the following string properties.

PropertyDescription
codeUnitsIt returns an unmodified list of the UTF-16 code units of this string.
isEmptyIf the string is empty, it returns true.
LengthIt returns the length of the string including whitespace.

String Methods

The Dart provides an extensive range of methods. The list of a few essential methods is given below.

MethodsDescriptions
toLowerCase()It converts all characters of the given string in lowercase.
toUpperCase()It converts all characters of the given string in uppercase.
trim()It eliminates all whitespace from the given string.
compareTo()It compares one string from another.
replaceAll()It replaces all substring that matches the specified pattern with a given string.
split()It splits the string at matches of the specified delimiter and returns the list of the substring.
substring()It returns the substring from start index, inclusive to end index.
toString()It returns the string representation of the given object.
codeUnitAt()It returns the 16-bits code unit at the given index

Comments