2

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

Yuvaraj M
  • 433
  • 2
  • 13

5 Answers5

2

Use this:

string = [string stringByReplacingOccurrencesOfString:@"€" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"," withString:@""];
David Manpearl
  • 12,176
  • 8
  • 52
  • 71
Andrey Chernukha
  • 21,004
  • 16
  • 95
  • 160
2

You can try:

-(NSString*)cleanString:(NSString*)str
    NSString *string = [NSString stringWithString:str];
    NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"€,"];
    string = [string stringByTrimmingCharactersInSet:charSet];
    return string;
}
woz
  • 10,805
  • 3
  • 33
  • 64
janusfidel
  • 7,929
  • 4
  • 28
  • 51
1

Best solution is Remove all but numbers from NSString answer.

Alternative for fixed cleaning in string is:

string = [[string stringByReplacingOccurencesOfString:@"€" withString:@""] stringByReplacingOccurencesOfString:@"," withString:@""];
Community
  • 1
  • 1
Paresh Navadiya
  • 37,791
  • 11
  • 79
  • 130
1
string = [string stringByReplacingOccurrencesOfString:@"€" withString:@""]; 

where string contains "€100,000,000"

Zoltan Toth
  • 46,038
  • 11
  • 115
  • 133
0

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 ","
Black Frog
  • 11,418
  • 1
  • 33
  • 66