我有以下字符串:
R $ 1.234.567,89
我需要它看起来像:1.234.567.89
我该怎么做?
这是我试过的:
NSString* cleanedString = [myString stringByReplacingOccurrencesOfString:@"." withString:@""];
cleanedString = [[cleanedString stringByReplacingOccurrencesOfString:@"," withString:@"."]
stringByTrimmingCharactersInSet: [NSCharacterSet symbolCharacterSet]];
它有效,但我认为必须有更好的方法。建议?
答案 0 :(得分:0)
如果您只想从字符串中删除前两个字符,可以执行此操作
NSString *cleanedString = [myString substringFromIndex:2];
答案 1 :(得分:0)
如果你的号码总是在$之后,但你之前有更多的字符,你可以这样做:
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