我有一个看起来像的字符串:
#chat :hi there
我想将:
中的所有文字扫描到一个字符串,因此它会像hi there
我试过
[[NSScanner scannerWithString:argument] scanUpToString:@":" intoString:&newarg];
但newarg
仅包含#chat
。如何实现这一目标?
答案 0 :(得分:4)
示例字符串:
#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,
你好吗?
答案 1 :(得分:0)
考虑使用if语句迭代扫描。您总是告诉计算机扫描所有内容,直到字符“:”,但听起来您实际上想要在“:”字符后扫描所有内容。安妮的答案提供了一个很好的例子。