我有一個字符串,我想搜索以「#」開頭並以「。」結尾的單詞(標籤)。或「」或‘’我發現這個網上,但是有限,因爲: - 你可以找到字符串中的一個字(雖然有更多的話) - ‘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