gpt4 book ai didi

objective-c - 如何使用 NSScanner 扫描字符串

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

我有一个看起来像这样的字符串:

#chat :hi there

我想将 : 中的所有文本扫描为字符串,因此它的结尾类似于 hi there

我已经尝试过

[[NSScanner scannerWithString:argument] scanUpToString:@":" intoString:&newarg];

但是newarg仅包含#chat。如何实现这一目标?

最佳答案

示例字符串:

#chat :Hello World,
#chat :How are you doing?

代码:

NSString *theString =   @"#chat :Hello World,\n"
"#chat :How are you doing?";

NSScanner *theScanner = [NSScanner scannerWithString:theString];
NSCharacterSet *seperator = [NSCharacterSet characterSetWithCharactersInString:@":"];
NSCharacterSet *newLine = [NSCharacterSet newlineCharacterSet];
NSString *theText;

while ([theScanner isAtEnd] == NO) {

[theScanner scanUpToCharactersFromSet:seperator intoString:NULL];
[theScanner setScanLocation: [theScanner scanLocation]+1];
[theScanner scanUpToCharactersFromSet:newLine intoString:&theText];

NSLog(@"%@",theText);

}

输出:

Hello World,
How are you doing?

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

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