如何给 NSString 添加百分号

发布于 2024-07-17 02:13:52 字数 159 浏览 13 评论 0原文

我想在字符串中的数字后面有一个百分号。 像这样: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

江南烟雨〆相思醉 2024-07-24 02:13:53

iOS 9.2.1、Xcode 7.2.1、启用 ARC

您始终可以在要附加的字符串中单独附加“%”,而无需任何其他格式说明符,如下所示...

int test = 10;

NSString *stringTest = [NSString stringWithFormat:@"%d", test];
stringTest = [stringTest stringByAppendingString:@"%"];
NSLog(@"%@", stringTest);

对于 iOS7.0+

要将答案扩展到可能导致冲突的其他字符,您可以选择使用:

- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters

逐步写出,如下所示:

int test = 10;

NSString *stringTest = [NSString stringWithFormat:@"%d", test];
stringTest = [[stringTest stringByAppendingString:@"%"] 
             stringByAddingPercentEncodingWithAllowedCharacters:
             [NSCharacterSet alphanumericCharacterSet]];
stringTest = [stringTest stringByRemovingPercentEncoding];

NSLog(@"percent value of test: %@", stringTest);

或者简短hand:

NSLog(@"percent value of test: %@", [[[[NSString stringWithFormat:@"%d", test] 
stringByAppendingString:@"%"] stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]] stringByRemovingPercentEncoding]);

感谢所有原始贡献者。 希望这可以帮助。 干杯!

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...

int test = 10;

NSString *stringTest = [NSString stringWithFormat:@"%d", test];
stringTest = [stringTest stringByAppendingString:@"%"];
NSLog(@"%@", stringTest);

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:

int test = 10;

NSString *stringTest = [NSString stringWithFormat:@"%d", test];
stringTest = [[stringTest stringByAppendingString:@"%"] 
             stringByAddingPercentEncodingWithAllowedCharacters:
             [NSCharacterSet alphanumericCharacterSet]];
stringTest = [stringTest stringByRemovingPercentEncoding];

NSLog(@"percent value of test: %@", stringTest);

Or short hand:

NSLog(@"percent value of test: %@", [[[[NSString stringWithFormat:@"%d", test] 
stringByAppendingString:@"%"] stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]] stringByRemovingPercentEncoding]);

Thanks to all the original contributors. Hope this helps. Cheers!

耳钉梦 2024-07-24 02:13:52

NSString 格式的百分号代码为 %%。 对于 NSLog()printf() 格式也是如此。

The code for percent sign in NSString format is %%. This is also true for NSLog() and printf() formats.

还在原地等你 2024-07-24 02:13:52

百分号的转义代码是“%%”,因此您的代码将如下所示

[NSString stringWithFormat:@"%d%%", someDigit];

此外,所有其他格式说明符都可以在 概念字符串文章

The escape code for a percent sign is "%%", so your code would look like this

[NSString stringWithFormat:@"%d%%", someDigit];

Also, all the other format specifiers can be found at Conceptual Strings Articles

昔日梦未散 2024-07-24 02:13:52

如果这在某些情况下有帮助,可以使用 unicode 字符:

NSLog(@"Test percentage \uFF05");

If that helps in some cases, it is possible to use the unicode character:

NSLog(@"Test percentage \uFF05");
一百个冬季 2024-07-24 02:13:52

接受的答案不适用于 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.

执手闯天涯 2024-07-24 02:13:52

似乎如果 %% 后面跟着 %@NSString 会转到一些奇怪的代码
试试这个,这对我有用

NSString *str = [NSString stringWithFormat:@"%@%@%@", @"%%", 
                 [textfield text], @"%%"]; 

seems if %% followed with a %@, the NSString will go to some strange codes
try this and this worked for me

NSString *str = [NSString stringWithFormat:@"%@%@%@", @"%%", 
                 [textfield text], @"%%"]; 
纸伞微斜 2024-07-24 02:13:52

使用以下代码。

 NSString *searchText = @"Bhupi"
 NSString *formatedSearchText = [NSString stringWithFormat:@"%%%@%%",searchText];

将输出:%Bhupi%

uese following code.

 NSString *searchText = @"Bhupi"
 NSString *formatedSearchText = [NSString stringWithFormat:@"%%%@%%",searchText];

will output: %Bhupi%

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文