我有以下字符串:
R $ 1.234.567,89
我需要它看起来像:1.234.567.89
我怎样才能做到这一点?
这是我尝试的:
NSString* cleanedString = [myString stringByReplacingOccurrencesOfString:@"." withString:@""];
cleanedString = [[cleanedString stringByReplacingOccurrencesOfString:@"," withString:@"."]
stringByTrimmingCharactersInSet: [NSCharacterSet symbolCharacterSet]];
它有效,但是我认为必须有更好的方法。有什么建议吗?
如果您的数字始终在$之后,但是前面有更多字符,则可以这样设置:
NSString* test = @"R$1.234.567,89";
NSString* test2 = @"TESTERR$1.234.567,89";
NSString* test3 = @"HEllo123344R$1.234.567,89";
NSLog(@"%@",[self makeCleanedText:test]);
NSLog(@"%@",[self makeCleanedText:test2]);
NSLog(@"%@",[self makeCleanedText:test3]);
方法是:
- (NSString*) makeCleanedText:(NSString*) text{
int indexFrom = 0;
for (NSInteger charIdx=0; charIdx<[text length]; charIdx++)
if ( '$' == [text characterAtIndex:charIdx])
indexFrom = charIdx + 1;
text = [text stringByReplacingOccurrencesOfString:@"," withString:@"."];
return [text substringFromIndex:indexFrom];
}
结果是:
2013-10-20 22:35:39.726 test[40546:60b] 1.234.567.89
2013-10-20 22:35:39.728 test[40546:60b] 1.234.567.89
2013-10-20 22:35:39.731 test[40546:60b] 1.234.567.89
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句