简体   繁体   English

Objective-C-如何从字符串中删除字符?

[英]Objective-C - How To Remove Characters From a String?

I have a UILabel that has a formatted String (formatted for currency), so there is a dollar sign, $21.34. 我有一个具有格式字符串(格式化为货币)的UILabel,所以有一个美元符号$ 21.34。

In the core data entity the attribute is of a type double, I am using an NSDecimalNumber to save to the database. 在核心数据实体中,属性的类型为double,我正在使用NSDecimalNumber保存到数据库。

    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. 无论如何,我需要不是NaN,所以我的想法是删除美元符号,但我不知道该怎么做,否则也许有更好的方法可以实现我的目标。

    NSString* cleanedString = [costString stringByTrimmingCharactersInSet: [NSCharacterSet symbolCharacterSet]];

比上面的代码要健壮得多(而且我认为它也可以处理逗号)

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;

To add to @justin's answer: 要添加到@justin的答案中:

[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 (eg £1,005.33 becomes 1005.33 ) 得到一个适合算术的干净数字(例如£1,005.33变成1005.33

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. 您可以使用NSNumberFormatter从值到字符串,就像您说的那样,但是您不应该依赖字符串作为该值的唯一来源。

Also, NSDecimalNumber is probably overkill for representing something like currency. 另外,NSDecimalNumber对于表示货币之类的东西可能会过大。 NSDecimalNumber is better suited for dealing with numbers in scientific notation. NSDecimalNumber更适合处理科学计数形式的数字。

A simple NSNumber object will be good enough for working with currency, using something like numberWithDouble: . 使用numberWithDouble:这样的简单NSNumber对象就足以处理货币。

Combining @justin's and @leafy's answer: 结合@justin和@leafy的答案:

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

This will give you 1000. 这将给您1000。

尝试这个:

NSDecimalNumber *newAmount = [[NSDecimalNumber alloc] initWithString:[string substringFromIndex:1]];

If you need to remove the $ sign. 如果需要删除$符号。 why not just do the following before dealing with the number 为什么不只在处理数字之前执行以下操作

NSString *newStringValue = [inputString stringByReplacingOccurrencesOfString:@"$" withString:@""];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM