Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in method to pad a string?

Tags:

dart

I'm trying to add zero-padding to a number. I did some searching and can't find an obvious way to do this. Am I missing something?

like image 483
w.brian Avatar asked Mar 04 '13 03:03

w.brian


People also ask

How do I pad a string in Javascript?

padStart() The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.

What is padding in string?

Inserting non-informative characters into a string is known as Padding. The insertion can be done anywhere on the string. String padding is useful for output formatting and alignment of strings while printing.

How do you add padding to a string in Python?

The standard way to add padding to a string in Python is using the str. rjust() function. It takes the width and padding to be used. If no padding is specified, the default padding of ASCII space is used.


1 Answers

You can use the String function .padLeft(Int width, [String = ' '])

https://api.dartlang.org/apidocs/channels/dev/dartdoc-viewer/dart-core.String#id_padLeft

int i = 2;
print(i.toString().padLeft(2, "0"));

result: 02

like image 84
Peril Ravine Avatar answered Oct 20 '22 10:10

Peril Ravine