gpt4 book ai didi

objective-c - 使用 nsscanner 类扫描字符串

转载 作者:太空狗 更新时间:2023-10-30 03:58:34 24 4
gpt4 key购买 nike

我要扫描这个字符串

"hello I am emp 1313 object of string class 123"

所以在这里我想知道它们是否存在任何整数值,如果存在我想为此显示它们我正在使用 NSScanner 类,这是我的代码 View

NSString *str = @" hello I am emp 1313 object of string class 123";

NSString *limit = @" object";
NSScanner *scanner = [NSScanner scannerWithString:str];

int i;
[scanner scanInt:&i];
NSString *output;
[scanner scanUpToString:limit intoString:&output];
NSLog(@"%d",i);

但问题是我做不到,我只想使用 NSScanner 类,所以各位专家能否就此给我一些建议......

最佳答案

试一试:

NSString *str = @" hello i am emp 1313 object of string class 123";
NSScanner *scanner = [NSScanner scannerWithString:str];

// set it to skip non-numeric characters
[scanner setCharactersToBeSkipped:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];

int i;
while ([scanner scanInt:&i])
{
NSLog(@"Found int: %d",i);
}

// reset the scanner to skip numeric characters
[scanner setScanLocation:0];
[scanner setCharactersToBeSkipped:[NSCharacterSet decimalDigitCharacterSet]];

NSString *resultString;
while ([scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&resultString]) {
NSLog(@"Found string: %@",resultString);
}

输出:

2010-10-27 14:40:39.137 so[2482:a0f] Found int: 1313
2010-10-27 14:40:39.140 so[2482:a0f] Found int: 123
2010-10-27 14:40:39.141 so[2482:a0f] Found string: hello i am emp
2010-10-27 14:40:39.141 so[2482:a0f] Found string: object of string class

关于objective-c - 使用 nsscanner 类扫描字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4032366/

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