0

How to add zeros in front of a string in Objective-C so that the length of the new string will be equal to a specific value? For example, if the specific length is 6,

  1 --> 000001
 12 --> 000012
123 --> 000123
...

Besides, I don't want to use loops. More elegant, more pleasure.

1
  • [NSString stringWithFormat:@"%06ld", yourNumber]; Commented Apr 11, 2018 at 10:06

1 Answer 1

0

I can't actually think an easier solution than this:

NSInteger yourNumber = 12; // or your random number...
NSLog(@"%@", [NSString stringWithFormat:@"%06ld", yourNumber]);

That presents the output on the console:

000012
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.