Removing Character issue from NSString in iPhone

无人久伴 提交于 2019-12-12 21:00:50

问题


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 ','.


回答1:


You can try:

-(NSString*)cleanString:(NSString*)str
    NSString *string = [NSString stringWithString:str];
    NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"€,"];
    string = [string stringByTrimmingCharactersInSet:charSet];
    return string;
}



回答2:


Use this:

string = [string stringByReplacingOccurrencesOfString:@"€" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"," withString:@""];



回答3:


Best solution is Remove all but numbers from NSString answer.

Alternative for fixed cleaning in string is:

string = [[string stringByReplacingOccurencesOfString:@"€" withString:@""] stringByReplacingOccurencesOfString:@"," withString:@""];



回答4:


string = [string stringByReplacingOccurrencesOfString:@"€" withString:@""]; 

where string contains "€100,000,000"




回答5:


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 ","


来源:https://stackoverflow.com/questions/12281181/removing-character-issue-from-nsstring-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!