我有一个字符串,我想搜索以“#”开头并以“。”结尾的单词(标签)。或“”或‘’我发现这个网上,但是有限,因为: - 你可以找到字符串中的一个字(虽然有更多的话) - ‘RangeOfString’不允许多个选择在字符串中搜索标签
NSString *stringText = @"test #hello #world";
NSString *result = nil;
// Determine "#"
NSRange hashRange = [stringText rangeOfString:@"#" options:NSCaseInsensitiveSearch];
if (hashRange.location != NSNotFound)
{
// Determine " " location according to "#" location
NSRange endHashRange;
endHashRange.location = hashRange.length + hashRange.location;
endHashRange.length = [stringText length] - endHashRange.location;
endHashRange = [stringText rangeOfString:@" " options:NSCaseInsensitiveSearch range:endHashRange];
if (endHashRange.location != NSNotFound)
{
// Tags found: retrieve string between them
hashRange.location += hashRange.length;
hashRange.length = endHashRange.location - hashRange.location;
result = [stringText substringWithRange:hashRange];
}
}
你有想法,我该怎么办?
谢谢!
谢谢,但我无法从比赛推断。他返回正确的单词数量,但是使用代码而不是单词。 – Vins 2012-04-27 15:48:53
@Vins看看编辑,这应该澄清从'matches'数组中获取实际字符串的方式。 – dasblinkenlight 2012-04-27 15:53:06
哇!非常感谢! – Vins 2012-04-27 15:56:10