gpt4 book ai didi

cocoa - 从 NSString 中提取数字

转载 作者:行者123 更新时间:2023-12-03 17:09:54 28 4
gpt4 key购买 nike

如何从定义如下的字符串中提取数字:

NSString *getNumber = @"Price138.50 Code112.250"

我需要从此字符串中提取 138.50112.25

最佳答案

使用 NSScanner,对 this Apple example 稍作修改:

NSCharacterSet *numberCharset = [NSCharacterSet characterSetWithCharactersInString:@"0123456789-"];
NSScanner *theScanner = [NSScanner scannerWithString:aString];
while (![theScanner isAtEnd]) {
// Eat non-digits and negative sign
[theScanner scanUpToCharactersFromSet:numberCharset
intoString:NULL];
float aFloat;
if ([theScanner scanFloat:&aFloat]) {
NSLog(@"Found %f", aFloat);
}
}

关于cocoa - 从 NSString 中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38641636/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com