如何给 NSString 添加百分号
我想在字符串中的数字后面有一个百分号。 像这样:75%。
我怎样才能做到这一点? 我尝试过:
[NSString stringWithFormat:@"%d\%", someDigit];
但这对我不起作用。
I want to have a percentage sign in my string after a digit. Something like this: 75%.
How can I have this done? I tried:
[NSString stringWithFormat:@"%d\%", someDigit];
But it didn't work for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
iOS 9.2.1、Xcode 7.2.1、启用 ARC
您始终可以在要附加的字符串中单独附加“%”,而无需任何其他格式说明符,如下所示...
对于 iOS7.0+
要将答案扩展到可能导致冲突的其他字符,您可以选择使用:
- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters
逐步写出,如下所示:
或者简短hand:
感谢所有原始贡献者。 希望这可以帮助。 干杯!
iOS 9.2.1, Xcode 7.2.1, ARC enabled
You can always append the '%' by itself without any other format specifiers in the string you are appending, like so...
For iOS7.0+
To expand the answer to other characters that might cause you conflict you may choose to use:
- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters
Written out step by step it looks like this:
Or short hand:
Thanks to all the original contributors. Hope this helps. Cheers!
NSString
格式的百分号代码为%%
。 对于NSLog()
和printf()
格式也是如此。The code for percent sign in
NSString
format is%%
. This is also true forNSLog()
andprintf()
formats.百分号的转义代码是“%%”,因此您的代码将如下所示
此外,所有其他格式说明符都可以在 概念字符串文章
The escape code for a percent sign is "%%", so your code would look like this
Also, all the other format specifiers can be found at Conceptual Strings Articles
如果这在某些情况下有帮助,可以使用 unicode 字符:
If that helps in some cases, it is possible to use the unicode character:
接受的答案不适用于 UILocalNotification。 由于某种原因,
%%%%
(4 个百分号)或 unicode 字符“\uFF05
”仅适用于此。回顾一下,在格式化字符串时,您可以使用
%%
。 但是,如果您的字符串是 UILocalNotification 的一部分,请使用%%%%
或\uFF05
。The accepted answer doesn't work for UILocalNotification. For some reason,
%%%%
(4 percent signs) or the unicode character '\uFF05
' only work for this.So to recap, when formatting your string you may use
%%
. However, if your string is part of a UILocalNotification, use%%%%
or\uFF05
.似乎如果
%%
后面跟着%@
,NSString
会转到一些奇怪的代码试试这个,这对我有用
seems if
%%
followed with a%@
, theNSString
will go to some strange codestry this and this worked for me
使用以下代码。
将输出:%Bhupi%
uese following code.
will output: %Bhupi%