free geoip
39

Working with Strings in Dart Programming

Working with strings in Dart is essential for any Flutter or Dart-based application development. Dart offers a robust set of…

Working with strings in Dart is essential for any Flutter or Dart-based application development. Dart offers a robust set of features to manipulate and interact with string data. In this tutorial, we will explore various string operations, including concatenation, interpolation, splitting, replacing, and more.

Working with Strings in Dart

Whether you’re building mobile apps with Flutter or server-side apps using Dart, handling string data effectively is crucial. Dart treats strings as a sequence of UTF-16 code units, and its String class comes packed with methods that simplify string manipulation.

Here is a complete Dart code example demonstrating various string operations:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
void main() {
// String Declaration
String name = "Aliendro";
String greeting = "Welcome to Dart!";
// Concatenation
String message = greeting + " " + name;
print("Concatenation: $message");
// String Interpolation
print("Interpolation: Hello, $name!");
// Multi-line String
String description = '''
Dart is a client-optimized language
for fast apps on any platform.
''';
print("Multi-line:\n$description");
// String Length
print("Length: ${name.length}");
// Uppercase and Lowercase
print("Uppercase: ${name.toUpperCase()}");
print("Lowercase: ${name.toLowerCase()}");
// Contains and Replace
print("Contains 'Ali': ${name.contains('Ali')}");
print("Replace: ${name.replaceAll('Ali', 'Ry')}");
// Substring and Split
print("Substring: ${name.substring(0, 4)}");
print("Split: ${greeting.split(' ')}");
// Trim
String messy = " Hello Dart! ";
print("Trimmed: '${messy.trim()}'");
}
void main() { // String Declaration String name = "Aliendro"; String greeting = "Welcome to Dart!"; // Concatenation String message = greeting + " " + name; print("Concatenation: $message"); // String Interpolation print("Interpolation: Hello, $name!"); // Multi-line String String description = ''' Dart is a client-optimized language for fast apps on any platform. '''; print("Multi-line:\n$description"); // String Length print("Length: ${name.length}"); // Uppercase and Lowercase print("Uppercase: ${name.toUpperCase()}"); print("Lowercase: ${name.toLowerCase()}"); // Contains and Replace print("Contains 'Ali': ${name.contains('Ali')}"); print("Replace: ${name.replaceAll('Ali', 'Ry')}"); // Substring and Split print("Substring: ${name.substring(0, 4)}"); print("Split: ${greeting.split(' ')}"); // Trim String messy = " Hello Dart! "; print("Trimmed: '${messy.trim()}'"); }
void main() {
  // String Declaration
  String name = "Aliendro";
  String greeting = "Welcome to Dart!";
  
  // Concatenation
  String message = greeting + " " + name;
  print("Concatenation: $message");

  // String Interpolation
  print("Interpolation: Hello, $name!");

  // Multi-line String
  String description = '''
  Dart is a client-optimized language
  for fast apps on any platform.
  ''';
  print("Multi-line:\n$description");

  // String Length
  print("Length: ${name.length}");

  // Uppercase and Lowercase
  print("Uppercase: ${name.toUpperCase()}");
  print("Lowercase: ${name.toLowerCase()}");

  // Contains and Replace
  print("Contains 'Ali': ${name.contains('Ali')}");
  print("Replace: ${name.replaceAll('Ali', 'Ry')}");

  // Substring and Split
  print("Substring: ${name.substring(0, 4)}");
  print("Split: ${greeting.split(' ')}");

  // Trim
  String messy = "  Hello Dart!  ";
  print("Trimmed: '${messy.trim()}'");
}

For a more advanced look at Dart string manipulation, including regular expressions and pattern matching, you can also refer to the official Dart documentation: Dart String API Docs

Understanding these core string operations is fundamental for data processing, UI formatting, and user interaction in Dart applications. Practicing these examples will strengthen your command over Dart syntax and programming structure.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *