我有以下代码用于从一些表示数字的字符串中删除多余的字符:
NSCharacterSet *charactersToKeep = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
NSCharacterSet *charactersToBeRemoved = [charactersToKeep invertedSet];
NSString *myString = [dictionary objectForKey:@"MyKey"];
myString = [myString stringByTrimmingCharactersInSet:charactersToBeRemoved];
myString = [myString stringByReplacingOccurrencesOfString:@"," withString:@""];
在 method 之后stringByTrimmingCharactersInSet:
,除了逗号之外的所有多余字符都被删除。stringByReplacingOccurencesOfString:
删除逗号后的调用。为什么第一次调用后没有删除?
以下是每行代码之后 myString 的示例:
myString = " $9,959 "
myString = "9,959"
myString = "9959"
我也试过
NSCharacterSet *charactersToBeRemoved = [NSCharacterSet decimalDigitCharacterSet];
并得到相同的结果。