-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,429
  • 3
  • 23
  • 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,048
  • 19
  • 36
  • 62
Amin Negm-Awad
  • 16,289
  • 3
  • 34
  • 50
0
confirm.balanceStr = [self.amountLbl.text replaceOccurencesOfString:@"$" 
                               withString:@"" 
                                  options:NULL
                                    range:NSMakeRange(0, [receiver length])];

Try this code...

prince
  • 814
  • 1
  • 8
  • 35
0
lbl.text = [[yourStringWithDollor componentsSeparatedByString:@" "] objectAtIndex:1];
Mohith
  • 96
  • 5