×

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 Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

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

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.

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.

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 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 Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 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 If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

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

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

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.

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.