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

发布于 2024-08-22 05:33:38 字数 701 浏览 6 评论 0原文

我有一个 UILabel,它有一个格式化字符串(针对货币格式化),因此有一个美元符号,$21.34。

在核心数据实体中,属性的类型为 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();
    }

无论如何,我需要它不为 NaN,所以我的想法是删除美元符号,但我不知道该怎么做,或者也许有更好的方法来实现我的目标。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

流绪微梦 2024-08-29 05:33:38
    NSString* cleanedString = [costString stringByTrimmingCharactersInSet: [NSCharacterSet symbolCharacterSet]];

比上面的强大得多(我认为它也能处理逗号)

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

Is a lot more robust than the one above (and I think it will handle commas, too)

嗼ふ静 2024-08-29 05:33:38

好的,所以我最终使用了这段代码,它对我有用

        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;

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;
我们只是彼此的过ke 2024-08-29 05:33:38

添加到@justin的答案:

[NSCharacterSet symbolCharacterSet]

不会删除逗号。因此,您可以将其与: 结合

stringByReplacingOccurrencesOfString

使用:

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

获得适合算术的干净数字(例如 £1,005.33 变为 1005.33

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)

路弥 2024-08-29 05:33:38

实际上,您应该拥有自己的价值,它是一个简单的数字,就像交易货币时的浮点数一样。那么你应该有一个代表你的值的字符串。您可以使用 NSNumberFormatter 从值获取字符串,就像您所说的那样,但您不应该依赖字符串作为该值的唯一来源。

另外,NSDecimalNumber 对于表示货币之类的东西可能有点过分了。 NSDecimalNumber 更适合处理科学记数法中的数字。

一个简单的 NSNumber 对象足以处理货币,使用诸如 numberWithDouble: 之类的东西。

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:.

落花随流水 2024-08-29 05:33:38

结合 @justin 和 @leafy 的答案:

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

这会给你 1000。

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

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

This will give you 1000.

人海汹涌 2024-08-29 05:33:38

如果需要删除$符号。为什么不在处理数字之前执行以下操作

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

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

NSString *newStringValue = [inputString stringByReplacingOccurrencesOfString:@"$" withString:@""];
幸福丶如此 2024-08-29 05:33:38

试试这个:

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

Try this:

NSDecimalNumber *newAmount = [[NSDecimalNumber alloc] initWithString:[string substringFromIndex:1]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文