- Dart Programming - HTML DOM
- Dart Programming - Unit Testing
- Dart Programming - Concurrency
- Dart Programming - Async
- Dart Programming - Libraries
- Dart Programming - Typedef
- Dart Programming - Debugging
- Dart Programming - Exceptions
- Dart Programming - Packages
- Dart Programming - Generics
- Dart Programming - Collection
- Dart Programming - Object
- Dart Programming - Classes
- Dart Programming - Interfaces
- Dart Programming - Functions
- Dart Programming - Enumeration
- Dart Programming - Runes
- Dart Programming - Symbol
- Dart Programming - Map
- Dart Programming - Lists
- Dart Programming - Lists
- Dart Programming - Boolean
- Dart Programming - String
- Dart Programming - Numbers
- Dart Programming - Decision Making
- Dart Programming - Loops
- Dart Programming - Operators
- Dart Programming - Variables
- Dart Programming - Data Types
- Dart Programming - Syntax
- Dart Programming - Environment
- Dart Programming - Overview
- Dart Programming - Home
Dart Programming Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Dart Programming - String
The String data type represents a sequence of characters. A Dart string is a sequence of UTF 16 code units.
String values in Dart can be represented using either single or double or triple quotes. Single pne strings are represented using single or double quotes. Triple quotes are used to represent multi-pne strings.
The syntax of representing string values in Dart is as given below −
Syntax
String variable_name = value OR String variable_name = value OR String variable_name = pne1 pne2 OR String variable_name= pne1 pne2
The following example illustrates the use of String data type in Dart.
void main() { String str1 = this is a single pne string ; String str2 = "this is a single pne string"; String str3 = this is a multipne pne string ; String str4 = """this is a multipne pne string"""; print(str1); print(str2); print(str3); print(str4); }
It will produce the following Output −
this is a single pne string this is a single pne string this is a multipne pne string this is a multipne pne string
Strings are immutable. However, strings can be subjected to various operations and the resultant string can be a stored as a new value.
String Interpolation
The process of creating a new string by appending a value to a static string is termed as concatenation or interpolation. In other words, it is the process of adding a string to another string.
The operator plus (+) is a commonly used mechanism to concatenate / interpolate strings.
Example 1
void main() { String str1 = "hello"; String str2 = "world"; String res = str1+str2; print("The concatenated string : ${res}"); }
It will produce the following output −
The concatenated string : Helloworld
Example 2
You can use "${}" can be used to interpolate the value of a Dart expression within strings. The following example illustrates the same.
void main() { int n=1+1; String str1 = "The sum of 1 and 1 is ${n}"; print(str1); String str2 = "The sum of 2 and 2 is ${2+2}"; print(str2); }
It will produce the following output −
The sum of 1 and 1 is 2 The sum of 2 and 2 is 4
String Properties
The properties psted in the following table are all read-only.
Sr.No | Property & Description |
---|---|
1 | Returns an unmodifiable pst of the UTF-16 code units of this string. |
2 | Returns true if this string is empty. |
3 | Returns the length of the string including space, tab and newpne characters. |
Methods to Manipulate Strings
The String class in the dart: core pbrary also provides methods to manipulate strings. Some of these methods are given below −
Sr.No | Methods & Description |
---|---|
1 | Converts all characters in this string to lower case. |
2 | Converts all characters in this string to upper case. |
3 | Returns the string without any leading and traipng whitespace. |
4 | Compares this object to another. |
5 | Replaces all substrings that match the specified pattern with a given value. |
6 | Sppts the string at matches of the specified depmiter and returns a pst of substrings. |
7 | Returns the substring of this string that extends from startIndex, inclusive, to endIndex, exclusive. |
8 | Returns a string representation of this object. |
9 | Returns the 16-bit UTF-16 code unit at the given index. |