Dart Data Types

Dart String

A UTF-16 code unit or character sequence is called a dart string. The text value is stored there. You can use single quotes or double quotations to form the string. The triple-quotes can be used to generate the multiline string. Because strings cannot be changed after they are created, they are immutable.

To declare a string in Dart, use the String keyword. The string declaration’s syntax is provided below.

Syntax:

				
					String msg = 'Welcome to JavaTpoint';  
or  
String msg1 = "This is double-quoted string example.";  
or  
String msg2 = ' ' ' line1  
line2  
line3'''  
				
			

Printing String

The string is printed on the screen using the print() function. Any expression, any other object, and a formatted message can all be included in the string. The ${expression} function in Dart allows you to embed the value into a string. Let’s examine the next illustration.

Example

				
					void main() {   
   String str1 = 'this is an example of a single-line string';   
   String str2 = "this is an example of a double-quotes multiline line string";   
   String str3 = """this is a multiline line   
string using the triple-quotes""";   
  
   var  a = 10;  
   var b = 20;  
   
   print(str1);  
   print(str2);   
   print(str3);   
  
 // We can add expression using the ${expression}.  
   print("The sum is  = ${a+b}");  
} void main() {   
   String str1 = 'this is an example of a single-line string';   
   String str2 = "this is an example of a double-quotes multiline line string";   
   String str3 = """this is a multiline line   
string using the triple-quotes""";   
  
   var  a = 10;  
   var b = 20;  
   
   print(str1);  
   print(str2);   
   print(str3);   
  
 // We can add expression using the ${expression}.  
   print("The sum is  = ${a+b}");  
} 
				
			

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 two strings are combined using the + or += operator. Here is an example of it.

Example

				
					void main() {   
   String str1 = 'Welcome To ';   
   String str2 = "JavaTpoint";   
   String str3 = str1+str2;  
  
   print(str3);   
}  
				
			

Output

				
					Welcome To JavaTpoint
				
			

String Interpolation

Adding a new value to an existing string allows you to manipulate it using a method called string interpolation. The string, including variables, interpolated expressions, and placeholders, can be evaluated using it. String interpolation is done using the ${expression}. The corresponding values are substituted for the expressions. Using the following example, let’s comprehend.

Example

				
					void main() {   
   String str1 = 'Hello ';   
   String str2 = "World!";   
   String str3 = str1+str2;  
     
 print(str3);   
  
   var x = 26;  
   var y = 10;  
  
   print("The result is  = ${x%y}");  
  
   var name = "Peter";  
   var roll_nu = 101;  
     
   print("My name is ${name}, my roll number is ${roll_nu}");  
}  
				
			

Output

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

Explanation

The code above declares two strings variables, concatenates them to form a new string, and prints the outcome.

Two integer-valued variables were created, the mod operation was carried out, and the result was produced via string interpolation.

As we have demonstrated in the example above, we can utilize the string interpolation as a placeholder.

String Properties

The string characteristics that the Dart offers are as follows.

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

------- text mukavu ====

Share this Doc

Dart String

Or copy link

Explore Topic