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.
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.
leftPad() method to left pad a string with zeros, by adding leading zeros to string. System.out.println( StringUtils.leftPad( "0123456789" , 10 , "0" ) );
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.
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.
This one pads left with 10 zeroes.
NSString *padded = [NSString stringWithFormat:@"Padded left with zeros: %010d", 65];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With