ios Objective-C 字符串格式化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7251454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:19:36  来源:igfitidea点击:

Objective-C string formatting

iphoneobjective-cios

提问by Ben

Possible Duplicates:
Format string, integer with leading zeros
String Formatting Tricks/Docs

可能的重复项:
格式化字符串、带前导零的整数
字符串格式化技巧/文档

I have a really simple question but didn′t find document about.

我有一个非常简单的问题,但没有找到相关文件。

I am using this code :

我正在使用此代码:

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

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

所以@"0%i.mp3" = 01.mp3, 02.mp3 ... 09.mp3 文件

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

我的问题是我有这个 mp3 的名字更长,比如 01-thefirstsong.mp3 , 02-mysecondsong.mp3 ... 15-mylastsong.mp3

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

我怎么能写这个像@"allmyfiles.mp3"?

Regards

问候

回答by Mike Hay

If you use this

如果你使用这个

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

filenamewill contain 01-thefirstsong.mp3.

filename将包含01-thefirstsong.mp3.

%@is the placeholder for objectiveC strings (NSString).

%@是objectiveC 字符串(NSString)的占位符。