Dart Strings, Literals and String Interpolation

3
17869
Dart String literals and interploation example
Literals and String Interpolation in Dart

In this tutorial, we will learn more about Dart Strings, Literals and String Interpolations. I have divided this tutorial into three different sections. Let’s learn one by one.

What are Dart Literals

The first thing we have to understand is “What are literals in Dart”. So the below given code has a String, Integer, Double and Boolean value. These are called Dart Literals. Not only in Dart but in any other programming language.

void main() {
  
 'Waheed';
  123456;
  405.60;
  true;
  
}

Now on the left we can assign the values to a Dart variable, int, double and boolean.

void main() {
 //Dart Literals  
 var name = 'Waheed';
 int taxNo = 123456;
 double score = 405.60;
 bool isEmployed = true;
  
}

Different Ways to Write Dart Literals

Now move on to the next concept. There are different ways to write Dart Literals. We will start from String Literals

void main() {
 // Diffenet ways to write Literals in Dart
 String fName='Waheed';
 String lName="Akhtar";
}

As you may have noticed the variable fName has a literal value enclosed within the single quote while variable lName has literal value enclosed within the double quotes. Dart allows us to write String literal values within a single quote or double quote.

Let’s take another example. We want ‘It’s about Wednesday night’ as a String literal but the compiler is not able to understand what we have defined as we have surrounded the string within single quotes and its considering “It” as a string, rest of the part is not identified as part of the string.

void main() {
 // Diffenet ways to write Literals in Dart
 String fName='Waheed';
 String lName="Akhtar";
 String moment='It's about Wednesday night'
}

We have to let the compiler now that it’s not the end of the String. For that, we use escape character which is backslash. Everything looks fine now.

void main() {
 // Diffenet ways to write Literals in Dart
 String fName='Waheed';
 String lName="Akhtar";
 String moment='It\'s about Wednesday night';
}

In Dart we have another way to write apostrophe string literals. Simple put the apostrophe String in double quotes. Whatever is written inside the double quotes is considered as String.

void main() {
 // Diffenet ways to write Literals in Dart
 String fName='Waheed';
 String lName="Akhtar";
 String moment="It's about Wednesday night";
}

Let’s take another example. We have a bored as a String variable with a long literal value. Well in programming languages it is not recommended to write a long value that you have to move horizontally.

void main() {
 String bored=' I am so bored but I am learning this amazing language. Dart is such a fun language.';
}

In general what we do is to break the line into two parts and concatenate them with a + symbol.

void main() {
 String bored=' I am so bored but I am learning this amazing language.'+
 'Dart is such a fun language.';
}

In dart we don’t have to use the + symbol to combine two Strings.

void main() {
 String bored=' I am so bored but I am learning this amazing language.'
 'Dart is such a fun language.';
}

Dart String Interpolation

It’s time to go through Dart String Interpolation concepts. Again consider an example.

void main() {
  String name="Waheed";
  String info="I am "+ name;
  print(info);
}

Here is the output. Which is totally fine.

I am Waheed

According to Dart programming convention using + symbol is considered bad programming practice as it is against the convention. Here comes the concept of String Interpolation.

Within the double quotes or single quote we can get the value of name variable. Using the name variable and adding a $ as prefix.


Run the code again and the output is same.

I am Waheed

Let’s move one step further and print out the number of characters literal Waheed has. Our traditional approach will be

void main() {
  String name="Waheed";
  print("The number of characters Waheed has is "+name.length);
}

Well, In Dart we will get an error message. Error: The argument type ‘int’ can’t be assigned to the parameter type ‘String’. In order to make this work, we have to convert the name.length into a String.

void main() {
  String name="Waheed";
  print("The number of characters Waheed has is "+name.length.toString());
}

and the output is

The number of characters Waheed has is 6

Again it is not following the Dart convention. Let’s do this by following String interpolation.

void main() {
  String name='Waheed';
  print("The number of characters Waheed has is ${name.length}");
}

The result is pretty much the same but it is under the dart convention.

The number of characters Waheed has is 6

Interpolation is not limited to just String. We can use interpolation for integer and double values as well. Here is quick example.

void main() {
  
 int a=30;
 int b=40;
 print("The sum of $a and $b is  ${a+b}");
   
}

The code execution will return

The sum of 30 and 40 is  70

I hope now you have pretty much very clear concepts of Dart Strings, Literals, and String Interpolation

3 COMMENTS

  1. I am new in Dart, and this is really amazing. thanks for the tutorial, however posting comments on your site is still a bit long process, I would suggest making it possible to comment with your visitors’ google or facebook Id and keep this as a third option.

  2. I am new in Dart, and this is really amazing. thanks for the tutorial, however posting comments on your site is still a bit long process, I would suggest making it possible to comment with your visitors’ google or facebook Id and keep this as a third option.

  3. One thing that was missing for me is a multi-line string literal that’s used like this:
    ”’anystring
    in
    between, even with “quotes” :)”’

    you can use single quotes (”’) or double quotes (“””).

LEAVE A REPLY

Please enter your comment!
Please enter your name here