1

我有以下字符串:

1.234.567,89 雷亚尔

我需要它看起来像:1.234.567.89

我怎样才能做到这一点?

这是我尝试过的:

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

它有效,但我认为必须有更好的方法。建议?

4

2 回答 2

0

如果您只想从字符串中删除前两个字符,您可以这样做

NSString *cleanedString = [myString substringFromIndex:2];
于 2013-10-20T20:12:18.050 回答
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
于 2013-10-20T20:29:39.967 回答