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:
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() {
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.
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.
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.
Property | Description |
---|---|
codeUnits | It returns an unmodified list of the UTF-16 code units of this string. |
isEmpty | If the string is empty, it returns true. |
Length | It 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.
Methods | Descriptions |
---|---|
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
Post a Comment