Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding string to left

How can one pad a string to left in objective c. E.g if I have an integer value of 6 I want it to be displayed as 06.

I use stringByPaddingToLength: but it pads it to the right like 60.

Your help is much appreciated.

like image 541
TonyNeallon Avatar asked Jun 08 '09 10:06

TonyNeallon


People also ask

How do you add padding to a string?

Using str. 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.

What is left padding in Java?

leftPad() method to left pad a string with zeros, by adding leading zeros to string. System.out.println( StringUtils.leftPad( "0123456789" , 10 , "0" ) );

How do I add a left padding in Python?

The zfill() method inserts 0s to the beginning of the String. This method does padding to the right by inserting characters to the left of the String with 0s until the string is of a specific length.

How can I pad a string with zeros on the left?

To pad zeros to a string, use the str. zfill() method. It takes one argument: the final length of the string you want and pads the string with zeros to the left.


1 Answers

This one pads left with 10 zeroes.

NSString *padded = [NSString stringWithFormat:@"Padded left with zeros: %010d", 65]; 
like image 100
nevan king Avatar answered Oct 04 '22 03:10

nevan king