Removing Character issue from NSString in iPhone

2019-07-17 16:48发布

I have a string with value "€100,000,000". I want to remove the '€' and ',' at the same time. I am try to use [NSCharacterSet symbolSet] but this method only removes the '€' without removing the ','.

5条回答
Anthone
2楼-- · 2019-07-17 17:05

Commas are not symbols. It's part of the [NSCharacterSet punctuationCharacterSet]. But if you use that set it will remove the decimal place holder (period).

You will just have to do two operation to get the result you want.

  1. Use [NSCharacterSet symbolSet] which will take care of all symbols for currency.
  2. Use the stringByReplacingOccurencesOfStrin to remove only the ","
查看更多
老娘就宠你
3楼-- · 2019-07-17 17:10
string = [string stringByReplacingOccurrencesOfString:@"€" withString:@""]; 

where string contains "€100,000,000"

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-07-17 17:21

Best solution is Remove all but numbers from NSString answer.

Alternative for fixed cleaning in string is:

string = [[string stringByReplacingOccurencesOfString:@"€" withString:@""] stringByReplacingOccurencesOfString:@"," withString:@""];
查看更多
乱世女痞
5楼-- · 2019-07-17 17:23

You can try:

-(NSString*)cleanString:(NSString*)str
    NSString *string = [NSString stringWithString:str];
    NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"€,"];
    string = [string stringByTrimmingCharactersInSet:charSet];
    return string;
}
查看更多
戒情不戒烟
6楼-- · 2019-07-17 17:28

Use this:

string = [string stringByReplacingOccurrencesOfString:@"€" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"," withString:@""];
查看更多
登录 后发表回答