stringByReplacingOccurrencesOfString:不起作用(stringByReplacingOccurrencesOfString: doesn't work)

编程入门 行业动态 更新时间:2024-10-27 10:23:16
stringByReplacingOccurrencesOfString:不起作用(stringByReplacingOccurrencesOfString: doesn't work)

我希望我的输出为“23”,但它仍显示“123”。

NSString *valorTextField = [[NSString alloc] initWithFormat:@"123"]; [valorTextField stringByReplacingOccurrencesOfString:@"1" withString:@""]; NSLog(@"%@", valorTextField);

I want my output to be "23", but it still shows "123".

NSString *valorTextField = [[NSString alloc] initWithFormat:@"123"]; [valorTextField stringByReplacingOccurrencesOfString:@"1" withString:@""]; NSLog(@"%@", valorTextField);

最满意答案

stringByReplacingOccurrencesOfString返回一个字符串:

NSString *valorTextField = [[NSString alloc] initWithFormat:@"123"]; NSString *replaced = [valorTextField stringByReplacingOccurrencesOfString:@"1" withString:@""]; NSLog(@"%@", replaced);

或者,您可以使用NSMutableString并使用replaceOccurrencesOfString:withString:options:range:方法。

stringByReplacingOccurrencesOfString returns a new string:

NSString *valorTextField = [[NSString alloc] initWithFormat:@"123"]; NSString *replaced = [valorTextField stringByReplacingOccurrencesOfString:@"1" withString:@""]; NSLog(@"%@", replaced);

Alternatively, you can work with a NSMutableString and use the replaceOccurrencesOfString:withString:options:range: method.

更多推荐

本文发布于:2023-08-03 08:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   stringByReplacingOccurrencesOfString   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!