Objective-C string formatting

Possible duplicates:
Format string, integer with leading zeros
Tricks / Documents for formatting strings

I have a really simple question, but I did not find the document.

I am using this code:

NSString *fileName = [NSString stringWithFormat:@"0%i.mp3", i]; 

therefore the files @ "0% i.mp3" = 01.mp3, 02.mp3 ... 09.mp3

My problem is that I have this mp3 with a longer name, like 01-thefirstsong.mp3, 02-mysecondsong.mp3 ... 15-mylastsong.mp3

How can I write this as @ "allmyfiles.mp3"?

Hi

+6
source share
1 answer

If you use this

 int i = 1; NSString *name = @"thefirstsong"; NSString *filename = [NSString stringWithFormat:@"%0d-%@.mp3", i, name]; 

filename will contain 01-thefirstsong.mp3 .

%@ is a placeholder for integer integer strings (NSString).

+6
source

Source: https://habr.com/ru/post/896270/


All Articles