The syntax of String.padRight() method is:
String padRight(int width, [String padding = ' '])
This padRight() method of String pads this string on the right if it is shorter than width
.
Parameter | Optional/Required | Description |
---|---|---|
width | required | the total width to pad the string to |
padding | optional [default value is ' '] | the padding character used for padding (default is space) |
String.padRight() returns value of type String
.
In this example,
str
with the value 'Hello'.padRight()
method to pad the string to a total width of 10 characters.void main() {
String str = 'Hello';
String paddedStr = str.padRight(10);
print('Padded string: $paddedStr');
}
Padded string: Hello
In this example,
str
with the value 'ABC'.padRight()
method to pad the string to a total width of 5 characters, using '-' as the padding character.void main() {
String str = 'ABC';
String paddedStr = str.padRight(5, '-');
print('Padded string: $paddedStr');
}
Padded string: ABC--
In this example,
str
with the value 'Lorem ipsum'.padRight()
method to pad the string to a total width of 15 characters, using '*' as the padding character.void main() {
String str = 'Lorem ipsum';
String paddedStr = str.padRight(15, '*');
print('Padded string: $paddedStr');
}
Padded string: Lorem ipsum****
In this Dart tutorial, we learned about padRight() method of String: the syntax and few working examples with output and detailed explanation for each example.