stringOperations
In Dart, you can perform various string operations to manipulate and work with text data. Here are some common string operations in Dart:
1. Concatenation:
You can concatenate (combine) strings using the + operator or the += operator.
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // Using +
print(fullName); // "John Doe"
String message = "Hello, ";
message += "World!"; // Using +=
print(message); // "Hello, World!"2. Interpolation:
String interpolation allows you to embed expressions or variables within a string using ${}.
String name = "Alice";
int age = 30;
String greeting = "Hello, ${name}! You are ${age} years old.";
print(greeting); // "Hello, Alice! You are 30 years old."3. String Length:
You can get the length of a string using the length property.
4. Substring:
You can extract a portion of a string using the substring method.
5. Searching:
You can search for substrings within a string using contains, startsWith, and endsWith methods.
6. Splitting:
You can split a string into a list of substrings using the split method.
7. Trimming:
You can remove leading and trailing whitespace characters from a string using trim, trimLeft, and trimRight methods.
8. Converting to Upper/Lower Case:
You can convert a string to uppercase or lowercase using toUpperCase and toLowerCase methods.
These are some of the common string operations in Dart. String manipulation is an important part of many Dart applications, especially when dealing with user input and text processing.
Last updated