2013-10-20 71 views
1

我有以下字符串:如何将货币字符串转换为数字

R $ 1.234.567,89

我需要它看起来像:1.234.567.89

我怎样才能做到这一点?

这是我的尝试:

NSString* cleanedString = [myString stringByReplacingOccurrencesOfString:@"." withString:@""]; 
cleanedString = [[cleanedString stringByReplacingOccurrencesOfString:@"," withString:@"."] 
            stringByTrimmingCharactersInSet: [NSCharacterSet symbolCharacterSet]]; 

它的工作原理,但我认为必须有一个更好的办法。建议?

回答

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 
0

如果你只是想删除您的字符串的前两个字符,你可以做到这一点

NSString *cleanedString = [myString substringFromIndex:2]; 
相关问题