gpt4 book ai didi

cocoa - 使用 NSScanner 解析字符串

转载 作者:行者123 更新时间:2023-12-03 16:59:12 31 4
gpt4 key购买 nike

要解析的两行字符串是:

00:02.0 VGA compatible controller [0300]: Intel Corporation 82945G/GZ Integrated Graphics Controller [8086:2772] (rev 02)
00:02.1 Display controller [0380]: Intel Corporation 82945G/GZ Integrated Graphics Controller [8086:2776] (rev 02)

获取这些字符串:

(从第一行开始)

  • VGA 兼容 Controller
  • 英特尔公司 82945G/GZ 集成图形 Controller
  • [8086:2772]

(从第二行开始)

  • 显示 Controller
  • 英特尔公司 82945G/GZ 集成图形 Controller
  • [8086:2776]

现在我的起始代码是:

NSScanner *scn = [NSScanner scannerWithString:strtoparse];
NSString *devtype;
while ([scn isAtEnd] == NO)
{
if( [scn scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL] &&
[scn scanUpToString:@"[" intoString:&devtype]);
}

这不起作用。我什至无法从文档中理解扫描仪。那么有人可以发布工作代码吗?

最佳答案

快速片段:

NSString *theString = @"00:02.0 VGA compatible controller [0300]: Intel Corporation 82945G/GZ Integrated Graphics Controller [8086:2772] (rev 02)\n00:02.1 Display controller [0380]: Intel Corporation 82945G/GZ Integrated Graphics Controller [8086:2776] (rev 02)";

NSScanner *theScanner = [NSScanner scannerWithString:theString];

NSCharacterSet *space = [NSCharacterSet characterSetWithCharactersInString:@" "];
NSCharacterSet *bracket = [NSCharacterSet characterSetWithCharactersInString:@"["];
NSCharacterSet *linebreak = [NSCharacterSet newlineCharacterSet];

NSString *type;
NSString *name;
NSString *number;

while (![theScanner isAtEnd])
{
[theScanner scanUpToCharactersFromSet:space intoString:nil] ;
[theScanner scanUpToCharactersFromSet:bracket intoString:&type];
[theScanner scanUpToCharactersFromSet:space intoString:nil] ;
[theScanner scanUpToCharactersFromSet:bracket intoString:&name];
[theScanner scanUpToCharactersFromSet:space intoString:&number];
[theScanner scanUpToCharactersFromSet:linebreak intoString:nil] ;

NSLog(type);
NSLog(name);
NSLog(number);
}

输出:

VGA compatible controller 
Intel Corporation 82945G/GZ Integrated Graphics Controller
[8086:2772]
Display controller
Intel Corporation 82945G/GZ Integrated Graphics Controller
[8086:2776]

关于cocoa - 使用 NSScanner 解析字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6844911/

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