objective-c 如何将百分号添加到 NSString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/739682/
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-09-03 21:23:49  来源:igfitidea点击:

How to add percent sign to NSString

objective-cnsstringstring-literals

提问by Ilya Suzdalnitski

I want to have a percentage sign in my string after a digit. Something like this: 75%.

我想在一个数字后在我的字符串中有一个百分号。像这样:75%。

How can I have this done? I tried:

我怎么能做到这一点?我试过:

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

But it didn't work for me.

但它对我不起作用。

回答by mouviciel

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

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

回答by binarybob

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

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

回答by Resh32

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

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

NSLog(@"Test percentage \uFF05");

回答by JRam13

The accepted answer doesn't work for UILocalNotification. For some reason, %%%%(4 percent signs) or the unicode character '\uFF05' only work for this.

接受的答案不适用于 UILocalNotification。出于某种原因,%%%%(4 个百分号)或 unicode 字符 ' \uFF05' 仅适用于此。

So to recap, when formatting your string you may use %%. However, if your string is part of a UILocalNotification, use %%%%or \uFF05.

所以回顾一下,在格式化字符串时,您可以使用%%. 但是,如果您的字符串是 UILocalNotification 的一部分,请使用%%%%\uFF05

回答by user1483392

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

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

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

回答by Bhoopi

uese following code.

使用以下代码。

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

will output: %Bhupi%

将输出:%Bhupi%

回答by serge-k

iOS 9.2.1, Xcode 7.2.1, ARC enabled

iOS 9.2.1、Xcode 7.2.1、ARC 已启用

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+

适用于 iOS7.0+

To expand the answer to other characters that might cause you conflict you may choose to use:

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

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

- (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!

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