Dart Strings


In Dart, strings are a fundamental data type used to represent sequences of characters. They are widely used for storing and manipulating text, and Dart provides a rich set of features and methods for working with strings. Below is an overview of strings in Dart, including how to create, manipulate, and format them.

String Basics

Creating Strings

Strings in Dart can be created using single quotes ('), double quotes ("), or triple quotes (''' or """). The choice of quotes depends on the use case, especially when dealing with quotes inside the string.

  • Single and Double Quotes:
void main() { String singleQuoted = 'Hello, Dart!'; String doubleQuoted = "Hello, Dart!"; print(singleQuoted); // Output: Hello, Dart! print(doubleQuoted); // Output: Hello, Dart! }
  • Triple Quotes: Triple quotes are useful for multi-line strings or when the string contains both single and double quotes.
void main() { String multiLine = '''This is a string that spans multiple lines.'''; print(multiLine); }

String Interpolation

Dart supports string interpolation, which allows you to embed expressions within string literals. This is done using the dollar sign ($) followed by the variable name or the expression enclosed in curly braces ({}).

Example:

void main() { String name = 'Alice'; int age = 30; String greeting = 'Hello, $name! You are $age years old.'; print(greeting); // Output: Hello, Alice! You are 30 years old. String complexGreeting = 'Next year, you will be ${age + 1}.'; print(complexGreeting); // Output: Next year, you will be 31. }

String Methods

Dart provides a variety of built-in methods for manipulating strings. Here are some commonly used methods:

  • Length: Returns the number of characters in the string.
void main() { String text = 'Hello'; print(text.length); // Output: 5 }
  • Concatenation: You can concatenate strings using the + operator or the StringBuffer class for more efficient concatenation.
void main() { String part1 = 'Hello, '; String part2 = 'Dart!'; String concatenated = part1 + part2; // Using + operator print(concatenated); // Output: Hello, Dart! }
  • Substrings: Extracts a portion of the string.
void main() { String text = 'Hello, Dart!'; String sub = text.substring(7, 11); // Extracts 'Dart' print(sub); // Output: Dart }
  • Indexing: Access individual characters using their index (0-based).
void main() { String text = 'Hello'; print(text[1]); // Output: e }
  • Trim: Removes leading and trailing whitespace.
void main() { String text = ' Hello, Dart! '; print(text.trim()); // Output: 'Hello, Dart!' }
  • Replace: Replaces occurrences of a substring.
void main() { String text = 'Hello, Dart!'; String replaced = text.replaceAll('Dart', 'Flutter'); print(replaced); // Output: Hello, Flutter! }
  • Split: Splits the string into a list based on a delimiter.
void main() { String text = 'apple,banana,cherry'; List<String> fruits = text.split(','); print(fruits); // Output: [apple, banana, cherry] }

String Comparison

Strings can be compared using equality operators (== and !=). Dart is case-sensitive when comparing strings.

Example:

void main() { String str1 = 'Hello'; String str2 = 'hello'; print(str1 == str2); // Output: false print(str1 != str2); // Output: true }

Raw Strings

Dart allows the creation of raw strings using the r prefix. Raw strings treat backslashes (\) as literal characters and do not interpret escape sequences.

Example:

void main() { String rawString = r'This is a raw string with a backslash: \n'; print(rawString); // Output: This is a raw string with a backslash: \n }

Conclusion

Strings in Dart are versatile and powerful, providing numerous methods and features for text manipulation and formatting. With support for string interpolation, multi-line strings, and a variety of built-in methods, Dart makes it easy to work with textual data. Understanding how to effectively use strings is essential for developing robust Dart applications, as they play a crucial role in user interaction, data representation, and application logic.