Objective-C - How To Remove Characters From a Stri

2019-04-16 11:51发布

I have a UILabel that has a formatted String (formatted for currency), so there is a dollar sign, $21.34.

In the core data entity the attribute is of a type double, I am using an NSDecimalNumber to save to the database.

    self.purchase.name = self.nameTextField.text;
    NSString *string = self.amountLabel.text
    NSDecimalNumber *newAmount = [[NSDecimalNumber alloc] initWithString:string];
    NSLog(@"%@", string); // THIS RETURNS NaN, because of dollar sign i think

    NSManagedObjectContext *context = self.purchase.managedObjectContext;

    NSError *error = nil;
    if (![context save:&error]) 
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

Anyway, I need this to not be NaN, so my thinking is to remove the dollar sign, but i do not know how to do that, or perhaps there is a better way to accomplish my goal.

7条回答
We Are One
2楼-- · 2019-04-16 12:18

Combining @justin's and @leafy's answer:

[[@"$1,000" stringByReplacingOccurrencesOfString:@"," withString:@""] stringByTrimmingCharactersInSet: [NSCharacterSet symbolCharacterSet]];

This will give you 1000.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-16 12:19

OK, so i ended up using this code and it works for me

        NSString *inString = self.amountLabel.text;
    NSMutableString *outString = [NSMutableString stringWithCapacity:inString.length];

    NSScanner *scanner = [NSScanner scannerWithString:inString];
    NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];

    while ([scanner isAtEnd] == NO)
    {
        NSString *buffer;
        if ([scanner scanCharactersFromSet:numbers intoString:&buffer])
        {
            [outString appendString:buffer];
        }
        else {
            [scanner setScanLocation:([scanner scanLocation] + 1)];
        }

    }
    NSLog(@"%@", outString);
    NSDecimalNumber *amount = [NSDecimalNumber decimalNumberWithString:outString];

    self.purchase.amount = amount;
查看更多
▲ chillily
4楼-- · 2019-04-16 12:25

If you need to remove the $ sign. why not just do the following before dealing with the number

NSString *newStringValue = [inputString stringByReplacingOccurrencesOfString:@"$" withString:@""];
查看更多
Lonely孤独者°
5楼-- · 2019-04-16 12:39

Really you should have your value which is a simple number, like a float if dealing in currency. Then you should have a string that represents your value. You could use an NSNumberFormatter to get from the value to the string, like you say you have, but you shouldn't be relying on the string as your only source of that value.

Also, NSDecimalNumber is probably overkill for representing something like currency. NSDecimalNumber is better suited for dealing with numbers in scientific notation.

A simple NSNumber object will be good enough for working with currency, using something like numberWithDouble:.

查看更多
我命由我不由天
6楼-- · 2019-04-16 12:42

Try this:

NSDecimalNumber *newAmount = [[NSDecimalNumber alloc] initWithString:[string substringFromIndex:1]];
查看更多
我想做一个坏孩纸
7楼-- · 2019-04-16 12:43

To add to @justin's answer:

[NSCharacterSet symbolCharacterSet]

won't remove commas. So you can combine it with:

stringByReplacingOccurrencesOfString

and use:

    NSString* cleanedString = [[costString stringByReplacingOccurrencesOfString:@"," withString:@""] 
stringByTrimmingCharactersInSet: [NSCharacterSet symbolCharacterSet]];

to get a cleaned number suitable for arithmetic (e.g. £1,005.33 becomes 1005.33)

查看更多
登录 后发表回答