×

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( );

Related Topics

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Future and Stream

Before understanding Future and Stream in Dart, we need first to get familiar with two terms : Synchronous and Asynchronous Programming.  In synchronous programming, a single operation is handled at a...

9 minutes read.

Dart Sets

Sets data type in Dart A set is an unordered collection of items that are unique. Dart infers that sets tale strings as values. Example, var languages = { ‘Dart’ , ‘Flutter’, ‘Python’,...

3 minutes read.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Dart Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.