Dart padLeft and padRight examples

Published by admin on

Introduction :

In this tutorial, we will learn how to use padLeft and padRight methods in dart with different examples. Both of these methods are used to pad a string on the left or right.

 

padLeft :

This method is defined as below :

String padLeft(int w,[String padding = ' '])

It takes two parameters. The first parameter w is the width of the final string. It pads the string on the left if it is shorter than the given width w.

This method returns one new string. If the width is smaller or equal to the length of the string, it will return the same string. If the value is negative, it will consider this value as zero.

We can also add the different character as the padding. Make sure that the padding character has length 1. For characters like andΒ \u{10002}, it will produce a different result.

 

Example of padLeft without blank space as the pad character :

import 'dart:io';

void main() {
  String mainString, finalString;
  int finalLength;

  print("Enter a string : ");
  mainString = stdin.readLineSync();

  print("Enter the final length : ");
  finalLength = int.parse(stdin.readLineSync());
  finalString = mainString.padLeft(finalLength);

  print("Final string :${finalString} with length ${finalString.length}");
}

In this example, we are reading the string and the final length from the user and printing out the final string by padding blank spaces to the left. We are also printing the length of the string after padding is added to the left.

 

Examples :

Enter a string :
hello
Enter the final length :
10
Final string :     hello with length 10

Enter a string :
hello
Enter the final length :
15
Final string :          hello with length 15

As you can see here, the final length of the string is equal to the length we have entered.

 

Example of padLeft with a character :

We can also use padLeft with any other character as the padding character. For example :

void main() {
  String mainString = "hello";


  String finalString1 = mainString.padLeft(10,"*");
  String finalString2 = mainString.padLeft(10,"\u{10002}");

  print("Final string 1 :${finalString1} with length ${finalString1.length}");
  print("Final string 2 :${finalString2} with length ${finalString2.length}");
}

It will print the below output :

Final string 1 :*****hello with length 10
Final string 2 :𐀂𐀂𐀂𐀂𐀂hello with length 15

As I have mentioned before, the second example behaves differently with \u{10002}.

 

padRight :

padRight is similar to padLeft. The only difference is that it pads the characters to the right of a string. It is defined as below :

String padRight(int w,[String padding = ' '])

It takes the width wasΒ the first parameter and padding as the second parameter. The second parameter is optional. It pads the string on the right and returns the modified string.

For example :

void main() {
  String mainString = "hello";

  String finalString1 = mainString.padRight(10);
  String finalString2 = mainString.padRight(10, "*");
  String finalString3 = mainString.padRight(10, "\u{10002}");

  print("Final string 1 :${finalString1} with length ${finalString1.length}");
  print("Final string 2 :${finalString2} with length ${finalString2.length}");
  print("Final string 3 :${finalString3} with length ${finalString3.length}");
}

It will print the below output :

Final string 1 :hello      with length 10
Final string 2 :hello***** with length 10
Final string 3 :hello𐀂𐀂𐀂𐀂𐀂 with length 15

The first string pads only blank spaces to the right, the second string pads * to the right and the third-string prints a different length, just like the above padLeft example.

Try to go through the examples above and drop one comment below if you have any queries.

Categories: dart

0 Comments

Leave a Reply

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