×

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 Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 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.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

3 minutes 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 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 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 Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

5 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 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 Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

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 (‘  ’)...

3 minutes read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 minutes read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart Static Members

Static Members in Dart Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :  All the objects of the class having...

3 minutes read.