An Introduction to Dart String Operations
One of the most commonly used data types in Dart is the String data type, which represents a sequence of characters.
String operations are a fundamental part of Dart programming and are used to manipulate and transform strings in various ways.
In this tutorial, we will discuss some of the most common string operations in Dart.
String Concatenation
Concatenation is the process of joining two or more strings together. In Dart, the concatenation operator is the + symbol.
Here is an example of how to concatenate two strings in Dart
void main() {
String firstName = "Jock";
String lastName = "Rimold";
String result = firstName+ " " +lastName;
print(result);
}
Output
Jock Rimold
Use interpolation to compose strings and values
Let’s re-write the code as below (recommended)
void main() {
String firstName = "Jock";
String lastName = "Rimold";
String result = '$firstName $lastName';
print(result);
}
Dart String interpolation
Dart String interpolation is a way to embed expressions into a string to create a new string. To use interpolation, you simply include the expression inside curly braces within the string, preceded by a dollar sign($).
For example, if you have a variable called name that contains the string “Nikky“, you can use string interpolation to create a new string that includes the value of the name variable like this:
String greeting = "Hello, $name!";
String interpolation example
void main() {
var name = "Nikky";
String greeting = "Hello, $name!";
print(greeting);
}
The resulting value of the greeting variable would be “Hello, Nikky”.
Output
Hello, Nikky!
You can also use more complex expressions inside the curly braces, including method calls, arithmetic operations, and conditional statements. For example:
void main() {
var score = 110;
String message = "Your score is ${score > 100 ? 'great' : 'okay'}!";
print(message);
}
Your score is great!
the value of the message variable would be “Your score is great!” if the score variable is greater than 100, and “Your score is okay!” if it is 100 or less.
String interpolation makes it easy to combine strings and values in a clear and concise way, and is a useful tool for building dynamic strings in Dart.
Finding length
The length of a string is the number of characters in the string. In Dart, you can get the length of a string using the length property. Here is an example:
void main() {
String str = "Hello World";
print(str.length);
}
The output of the program will be 11.
Substring
A substring is a part of a string. In Dart, you can get a substring of a string using the substring() method. The substring() method takes two arguments: the starting index and the ending index. Here is an example:
void main() {
String str = "Hello World";
String substring = str.substring(0, 5);
print(substring);
}
The output of this program will be “Hello”.
Replace
The replace() method is used to replace a substring with another substring. The replace() method takes two arguments: the substring to be replaced and the substring to replace it with. Here is an example:
void main() {
String str = "Hello World";
String newStr = str.replace("World", "Dart");
print(newStr);
}
The output of this program will be “Hello Dart”.
Split
The split() method is used to split a string into an array of substrings. The split() method takes one argument: the delimiter to use for splitting the string. Here is an example:
void main() {
String str = "Hello,World,Dart";
List<String> parts = str.split(",");
print(parts);
}
The output of this program will be [“Hello”, “World”, “Dart”].
Trim
The trim() method is used to remove whitespace from the beginning and end of a string. Here is an example:
void main() {
String str = " Hello World ";
String newStr = str.trim();
print(newStr);
}
The output of this program will be “Hello World”.
In conclusion, these are some of the most common string operations in Dart. With these string operations, you can manipulate and transform strings to meet your programming needs.