Strings are non-primitive data-types, which stores a sequence of characters. Ex – ‘apple’ is a string, its character sequence is a, p, p, l, e.

** remember: in Dart, we represent string with either double quotes (“ ”) or single quotes(‘’).

Strings are non-primitive?

Strings are a sequence of characters. It’s mean, strings are derived from characters. String is non-primitive & linear data type. A Dart string (String object) holds a sequence of UTF-16 code units. You can use either single or double quotes to create a string.

Creating a string

We can create strings in 2 ways –

  • Single Quotes(‘ ’) –
void main(){
  // String creation using single quotes
  var a=’dart’;
  print(a);
  print(a.runtimeType);
 
  String aa=’linux’;
  print(aa);
  print(aa.runtimeType);
}
output

Double Quotes(“ ”) –

void main(){
   // String creation using double quotes
  var b=”Dart”;
  print(b);
  print(b.runtimeType);
 
  String bb=”windows”;
  print(bb);
  print(bb.runtimeType);
 
  // declaring String variable ‘str’
  late String str;
  // initializing variable ‘str’
  str=”microcodes.in”;
  print(str);
}
output

Multi-line String

We can create multiline strings in Dart, to create a multi-line string: use a triple quote with either single or double quotation marks.

Code 1:

void main(){
  late String sch;
  sch=”’ school located in india
headmaster name gulati
they uses linux”’;
    
        print(sch);
}
output

code 2:

void main(){
  var laptop=””” laptop comany is Dell
  installed os was windows 10
  current we uses Ubuntu linux “””;
  print(laptop);
  print(‘variable type ->’);
  print(laptop.runtimeType);
}
output

** Remember: you can initialize a String variable value later, for this you need to use ‘late’ keyword.

  // declaring String variable ‘str’
  late String str;
  // initializing variable ‘str’
  str=”microcodes.in”;
  print(str);

Let’s make a code, which get String variable input from user and print output.

Code 3:

import ‘dart:io’;
void main(){
  String? str;
  print(“Enter String: “);
  str=stdin.readLineSync();
  print(‘String Variable -> $str’);
}
output

** remember: library ‘dart:io’ helps to get user inputs. Stdin allows both synchronous and asynchronous reads from the standard input stream. Mixing synchronous and asynchronous reads is undefined. readLineSync() function. To take input from the console you need to import a library, named dart:io from libraries of Dart.

Let’s make a code, which will print a string value and variable data-type also.

Code 4:

import ‘dart:io’;
void main(){
  var str;
  print(“Enter String: “);
  str=stdin.readLineSync();
  print(‘String Variable -> $str’);
  print(‘variable type: ‘);
  print(str.runtimeType);
}
output