Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

Dart Strings

Strings data type in Dart

A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’) or double quotes (“  ”).

Example,

var a = ‘This is a string’;

In order to print a string value stored in a variable along with some text, use string interpolation.

Example,

var c = ‘is a’;


print(‘Dart $c good language’);

Strings can be concatenated using ‘+’ operator. For example,

Consider the following code

import 'dart:io';
// strings concatenation
void main( )
{
  // initializing s1 and s2 with string values
var s1 = 'Welcome to ';
  var s2 = 'Javatpoint' ;
  
  // concatenation of strings
var s3 = s1 + s2;
  
  print( s3 );
}

Output :

Welcome to Javatpoint

Dart provides another unique feature to create multi-line string using triple quotes (‘‘‘     ’’’).

Example:

var s1 = ‘‘‘This depicts a 
             multi-line string’’’;

Program

Consider the following code in Dart to understand the above points more clearly,

void main()
{
    // initializing ques with string value using var 
    var ques = ' How are you finding Dart ?';
    var ans = ' easy ';
  
    // initializing multi line string
    var str = ''' Virat Kohli 
 is the captian of Indian cricket team''';
  
    // prints the string value of ques
    print( ques );
   
    // prints the text along with string value of ans. $ performs string interpolation
    print( '\n For me Dart is $ans \n' );
  
    print( str );
}

Output :

How are you finding Dart ?


 For me Dart is  easy


 Virat Kohli
 is the captain of Indian cricket team

Properties

PropertyDescription
codeUnitsIt returns a list of the UTF-16 code units of the given string.   
Implementation = string.codeUnits;
isEmptyChecks whether the string is empty and returns true or false accordingly.   
Implementation = string.isEmpty;
LengthCalculates and returns the length of the string including any space, tab or newline characters.   
Implementation = string.length;

Methods

MethodsDescription
toLowerCase( )Converts all characters of the given string to lower case and returns the new converted string.
Implementation = string.toLowerCase( );
toUppercase( )Converts all the characters of the given string to upper case and returns the new converted string.
Implementation = string.toUpperCase( );
trim( )Removes all the leading and trailing spaces from a given string and returns the new changed string. It doesn’t discard spaces between the two strings.
Implementation = string.trim( );
compareTo( )Compares the two strings for equality and returns 0, 1, -1 accordingly. Returns 0 when the strings are exactly equal Returns 1 when the given string is greater than the other Returns -1 when the other string is greater than the given string.
Implementation = int compareTo( string other);
replaceAll( )Checks whether the substring matches the specified pattern with a given value and replaces them with that value.   
Implementation = string.replaceAll( pattern from, string replace); Here, ‘ from ’ represents the string to be replaced and ‘ replace ’ represents the substitution string.
split( )Looks for the delimiter passed to it as an argument in the string and splits the given string from the point where that delimiter is present. It returns the list of substrings.
Implementation = string.split( Pattern pattern); Here, pattern represents the delimiter.
substring( )Returns the substring of the given string starting from the starting index (including) to the ending index (excluding). Remember that the indexes begin from zero.
Implementation = substring( int startIndex, [ int endIndex ]);
toString( )Converts an expression to string and returns the same.
Implementation = val.toString( );