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
Property | Description |
codeUnits | It returns a list of the UTF-16 code units of the given string. Implementation = string.codeUnits; |
isEmpty | Checks whether the string is empty and returns true or false accordingly. Implementation = string.isEmpty; |
Length | Calculates and returns the length of the string including any space, tab or newline characters. Implementation = string.length; |
Methods
Methods | Description |
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( ); |