Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Strings


Share

Dart Strings

A string is a piece of text, like a name, a sentence or an address.

In Dart, you write strings inside single quotes or double quotes:

Example

void main() {
  String a = 'Hello';
  String b = "Hello";
  print(a);
  print(b);
}
Try it Yourself »

Result:

Hello
Hello

Both styles give the same string.

Most Dart code uses single quotes.


Quotes Inside a String

You can use single quotes inside a string with double quotes, and double quotes inside a string with single quotes:

Example

void main() {
  print("It's a sunny day");
  print('She said "Hello"');
}
Try it Yourself »

Result:

It's a sunny day
She said "Hello"

Escape Characters

You can also put a backslash \ before a quote, like 'It\'s', so Dart does not read it as the end of the string.

A backslash followed by a letter or a sign is called an escape character.

It puts a special character in the string:

CodeResult
\'Single quote
\"Double quote
\\Backslash
\nNew line
\tTab
\$Dollar sign

Example

void main() {
  print('It\'s a sunny day');
  print('Line one\nLine two');
  print('Name:\tLiam');
  print('C:\\Users\\Liam');
  print('Price: \$20');
}
Try it Yourself »

Result:

It's a sunny day
Line one
Line two
Name: Liam
C:\Users\Liam
Price: $20


Multi-line Strings

To write a string over several lines, use three single quotes ''' or three double quotes """:

Example

void main() {
  var poem = '''Roses are red,
Violets are blue,
Dart is fun,
And so are you.''';
  print(poem);
}
Try it Yourself »

Result:

Roses are red,
Violets are blue,
Dart is fun,
And so are you.

Raw Strings

If you write r before a string, it becomes a raw string.

In a raw string, a backslash is just a backslash, and escape characters do not work:

Example

void main() {
  print(r'Hello\nWorld');
  print('Hello\nWorld');
}
Try it Yourself »

Result:

Hello\nWorld
Hello
World

Without r, Dart reads \n in the second line as a new line.


Join Strings

You can join two or more strings with the + operator.

This is called concatenation:

Example

void main() {
  var firstName = 'John';
  var lastName = 'Doe';
  var fullName = firstName + ' ' + lastName;
  print(fullName);
}
Try it Yourself »

Result:

John Doe

Use += to add text to the end of a string variable.

message += ' morning' is a short way to write message = message + ' morning':

Example

void main() {
  var message = 'Good';
  message += ' morning';
  print(message);
}
Try it Yourself »

Result:

Good morning

Tip: In Dart, it is more common to join strings with string interpolation, which you will learn in the next chapter.


String Length and Characters

The length property gives the number of characters in a string:

Example

void main() {
  var txt = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  print(txt.length);
}
Try it Yourself »

Result:

26

You can get one character with its index (its position number) inside square brackets [].

The first character has index 0, the second has index 1, and so on:

Example

void main() {
  var fruit = 'Banana';
  print(fruit[0]);
  print(fruit[2]);
  print(fruit[fruit.length - 1]);
}
Try it Yourself »

Result:

B
n
a

Strings in Dart cannot be changed after they are created, so you cannot replace a character like this:

Example

void main() {
  var fruit = 'Banana';
  fruit[0] = 'C';
  print(fruit);
}
Try it Yourself »

Result:

main.dart:3:8: Error: The operator '[]=' isn't defined for the type 'String'.
Try correcting the operator to an existing operator, or defining a '[]=' operator.
  fruit[0] = 'C';
       ^

Instead, you create a new string, for example with the string methods.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.