-2

How can I remove all dollar($) symbols from my NSString?

amountDataArray = 
    [[NSMutableArray alloc]
         initWithObjects:@"$ 10", @"$ 20", @"$ 30", @"$ 40", @"$ 50", @"$ 60", nil];

I am showing displaying string in UILabel:

confirm.balanceStr = self.amountLbl.text;

I am also setting amountLbl.text to confirm.balanceStr, but I don't want '$' in my string.

I want to show only amount like 20, 30 , 40 but not with the dollar, like $10, $20,

Dev2rights
  • 3,469
  • 3
  • 25
  • 42

4 Answers4

1

Try something like this

amountLbl.text = [confirm.balanceStr stringByReplacingOccurrencesOfString:@"$ " withString:@""];
Devang
  • 320
  • 3
  • 12
1

There are several solutions:

  1. String operations, if there is no doubt, that the string has the prefix @"$ ":

    NSString *valueString = [dollaredString substringFromIndex:2];

  2. Scanning. A little bit more robust, but more work to do.

  3. Number formatter.

Build a number formatter for your string and let it make the work. You simply have to confgure it to your format.

Ankur
  • 5,086
  • 19
  • 37
  • 62
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
0
confirm.balanceStr = [self.amountLbl.text replaceOccurencesOfString:@"$" 
                               withString:@"" 
                                  options:NULL
                                    range:NSMakeRange(0, [receiver length])];

Try this code...

prince
  • 854
  • 2
  • 9
  • 36
0
lbl.text = [[yourStringWithDollor componentsSeparatedByString:@" "] objectAtIndex:1];
Mohith
  • 96
  • 5