String Concatenation & Collections in Dart (Class 12)

String Concatenation and Collections

Today we are going to learn the String Concatenation and Collections concept. Let’s start:

 

String concatenation

In the last class, we learned to add values.  Now we are going to add two or more text values. This means we are going to join two or more strings. For example:

void main () {
  String str1 = “Pakistan”, str2 = “country”;
  print (str1+str2);
}

 

String concatenation is about String not other types of variables like int, bool, etc. As we saw in the above example we used the + operator for String Concatenation. Now the result will be Pakistancountry. To add space between Strings we can add String Literal with space in it as under:-

void main () {
  String str1 = “Pakistan”, str2 = “country”;
  print (str1+” “+str2);
}

Now the answer would be with space Pakistan country. Now, we can similarly add space as and when required.

 

Concatenate other types with String

It is a must In String Concatenation that entities should be String. We can add another type of variable in String concatenation as well. For this purpose, we will convert that type i.e. int, bool, etc. by adding .toString() with it to change it to String. For example:

void main () {
  String str1 = “Pakistan”, str2 = “country”;
  int no1 = 34, no2=50;
  bool isMarried = true;
  print (str1+no1.toString()+no2.toString()+isMarried.toString());
}

 

Collections

We have learned how we can put value in a box. We called value as a variable. To store 3 values we need to create 3 boxes. Like

int no=2, no1=3, no2=4;

 

We use collections to store multiple values. In other languages we use array and similarly, there is the concept of collection. It is similar to variables. The collection represents a group of values.  For example, we intend to make a contact numbers list so we can make it via collections. There are three types of collections:

List

Set

Map

 

Let’s explore them one by one.

 

List:

The list represents a chain. We use [ ] for list creation. It is also like data type. A simple example of a list is:

void main () {
  List list = [1,3,4,5];
}

 

The above list is called a list of integers. We use the List keyword for making a list. The list can have values of different data types. So we can make a list of String, a list of doubles, etc. We can also make a list containing values of different data types as well.

 

Var list

We can use also the var keyword to create a list as well like:-

void main () {
  var list = [“Pakistan”, 34, 3.1, true];
}

Try yourself in Dart Pad.

 

Specifying variable type

We can specify variable type in List in angle brackets as well. It is to specifically mention that we are going to create which type of list i.e. int, String, double, bool, dynamic type. Let’s see an example:

void main () {
  List<int> numList = [45, 50, 60, 70];
  List<String> numString = [“Pakistan”, “Country”];
}

 

Basically, there is a restriction to enter only specific data. It is more difficult than a generic list but this method is recommended by Dart and is prescribed as a best practice.

 

Set

We make a set to store unique values in curly brackets. In a list, the same elements can come but in a set values have to be unique. Let’s see the format of the set:-

void main () {
  Set set1 = {45, 50, 60, 70, Pakistan};
}

 

As we did in the list we can also specify variable types in a set like this which is recommended by Dart:-

void main () {
  Set<int> set1 = {45, 50, 60, 70};
}

 

We will learn the last type of collection i.e. Map in the next class. So practice all topics discussed in today’s class related to String Concatenation and Collections to become an expert in them.

 

ROBINA KOUSAR
Content Writer
**************************
You can get all Mobile App Development Classes link from here:-

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.