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 String Methods


Share

Dart String Methods

A method is an action that belongs to a value.

Strings have many built-in methods, and you call them with a dot after the string.

A string method never changes the original string.

It returns a new value instead:

Example

void main() {
  var txt = 'Hello World';
  print(txt.toUpperCase());
  print(txt.toLowerCase());
  print(txt);
}
Try it Yourself »

Result:

HELLO WORLD
hello world
Hello World

Search in a String

contains(), startsWith() and endsWith() return true or false:

Example

void main() {
  var email = 'liam@example.com';
  print(email.contains('@'));
  print(email.startsWith('liam'));
  print(email.endsWith('.org'));
}
Try it Yourself »

Result:

true
true
false

indexOf() returns the position of the first match, and -1 if there is no match:

Example

void main() {
  var txt = 'I like apples and bananas';
  print(txt.indexOf('apples'));
  print(txt.indexOf('a'));
  print(txt.lastIndexOf('a'));
  print(txt.indexOf('cherries'));
}
Try it Yourself »

Result:

7
7
23
-1

Note: All search methods are case-sensitive, so 'Dart'.contains('dart') is false.



Get Part of a String

substring(start, end) returns the characters from index start up to, but not including, index end.

If you leave out end, you get the rest of the string:

Example

void main() {
  var txt = 'Hello World';
  print(txt.substring(0, 5));
  print(txt.substring(6));
}
Try it Yourself »

Result:

Hello
World

Replace, Split and Trim

replaceAll() replaces every match with another string:

Example

void main() {
  var txt = 'I like cats. Cats are cute.';
  print(txt.replaceAll('cats', 'dogs'));
}
Try it Yourself »

Result:

I like dogs. Cats are cute.

The word Cats was not replaced, because it starts with a capital letter.

split() cuts a string into a list of strings, at every separator:

Example

void main() {
  var fruits = 'apple,banana,cherry';
  print(fruits.split(','));
}
Try it Yourself »

Result:

[apple, banana, cherry]

trim() removes spaces at the start and at the end of a string, which is useful for text that a user typed:

Example

void main() {
  var input = '   Liam   ';
  print('[' + input + ']');
  print('[' + input.trim() + ']');
}
Try it Yourself »

Result:

[   Liam   ]
[Liam]

Pad a String

padLeft() adds characters at the start until the string has the length you ask for:

Example

void main() {
  var orderId = '42';
  print(orderId.padLeft(5, '0'));
  print('Tea'.padRight(8, '.') + '3.50');
  print('Coffee'.padRight(8, '.') + '4.20');
}
Try it Yourself »

Result:

00042
Tea.....3.50
Coffee..4.20

String Methods Reference

This table lists common properties and methods of Dart strings.

In the examples, s is the string 'Hello World'.

Property or methodDescriptionExampleResult
lengthNumber of characterss.length11
isEmptytrue if the string has no characters''.isEmptytrue
isNotEmptytrue if the string has at least one characters.isNotEmptytrue
toUpperCase()Returns the string in upper cases.toUpperCase()HELLO WORLD
toLowerCase()Returns the string in lower cases.toLowerCase()hello world
contains()Checks if the string contains a values.contains('World')true
startsWith()Checks if the string starts with a values.startsWith('Hell')true
endsWith()Checks if the string ends with a values.endsWith('d')true
indexOf()Position of the first match, or -1s.indexOf('o')4
lastIndexOf()Position of the last match, or -1s.lastIndexOf('o')7
substring()Returns part of the strings.substring(0, 5)Hello
replaceAll()Replaces every matchs.replaceAll('o', '0')Hell0 W0rld
replaceFirst()Replaces the first match onlys.replaceFirst('o', '0')Hell0 World
split()Cuts the string into a lists.split(' ')[Hello, World]
trim()Removes spaces at both ends' Hi '.trim()Hi
trimLeft()Removes spaces at the start' Hi'.trimLeft()Hi
trimRight()Removes spaces at the end'Hi '.trimRight()Hi
padLeft()Adds characters at the start'7'.padLeft(3, '0')007
padRight()Adds characters at the end'7'.padRight(3, '0')700
compareTo()Compares two strings in alphabetical order: a negative number, 0 or a positive number'a'.compareTo('b')-1
*Repeats the string'ha' * 3hahaha


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.