开发者

NSString/NSMutableString strange behaviour

开发者 https://www.devze.com 2023-04-06 22:43 出处:网络
OK here is nsmutablestring data = [NSMutableString stringWithFormat:@\"&cb_games%5B%5D=\"]; Now when ever I try t开发者_运维知识库o print or use this string I get big number instead of %5B and

OK here is nsmutablestring

data = [NSMutableString stringWithFormat:@"&cb_games%5B%5D="];

Now when ever I try t开发者_运维知识库o print or use this string I get big number instead of %5B and %5D not sure why this is happeing any help would be apritiated

thanks


The reason you get unexpected output is that '%' is used as conversion specifier in printf and obviously NSLog and NSString formattings. You need to escape '%' if you don't want it to be interpreted as a conversion specifier. You can escape '%' by preceding it with another '%' like '%%'.

Your string should look like,

@"&cb_games%%5B%%5D="

And the @August Lilleaas's answer is also noteworthy.


Try this:

NSString * data = [NSMutableString stringWithFormat:@"&cb_games%%5B%%5D="];
NSLog(@"%@",data);


stringWithFormat is basically printf, and it attempts to replace your percentages with values that you haven't provided, which is why wierd stuff happens.

[NSMutableString stringWithFormat:@"Hello: %d", 123];
// @"Hello: 123"

If you want a mutable string from a string, try this:

[NSMutableString stringWithString:@"Abc %2 %3"];
// @"Abc %2 %3"


The % is used for string formatting and stuff. I imagine you need to escape the character or something, possibly with a slash.


Did you mean to write?

data = [NSMutableString stringWithString@"&cb_games%5B%5D="]
0

精彩评论

暂无评论...
验证码 换一张
取 消