[英]stringByReplacingOccurrencesOfString(" ", withString: "") doesn't work
[英]stringByReplacingOccurrencesOfString: doesn't work
我希望輸出為“ 23”,但仍顯示為“ 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:
方法。
您不會再次將返回的字符串分配給valorTextField
。 您只是在執行功能。 實際字符串不變。 而是寫:
valorTextField = [valorTextField stringByReplacingOccurrencesOfString:@"1" withString:@""];
NSLog(@"%@", valorTextField);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.