如何在目標 c 的句子中找到特定單詞的位置范圍?
可以說我有以下 NSString
NSString *stringOfHashTags = "mr#hello Mr.#hello"
我在用
NSString word=@"hello";
NSRange termsRange = [stringOfHashTags rangeOfString:[word substringFromIndex:[word rangeOfString:@"#"].location]];
查找以 # 開頭的單詞的位置,但如果該單詞在句子中存在兩次或多次,則僅回傳第一個位置。任何幫助表示贊賞。
uj5u.com熱心網友回復:
您可以使用enumerateSubstringsInRange(搜索和閱讀檔案始終是起點)。
簡單的例子:
NSString *aString = @"mr#hello Mr.#hello";
NSString *word = @"hello";
[aString enumerateSubstringsInRange:NSMakeRange(0, [aString length])
options:NSStringEnumerationByWords | NSStringEnumerationLocalized
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
if ([substring rangeOfString:word options:NSCaseInsensitiveSearch].location != NSNotFound) {
/* do whatever */;
NSLog(@"found at: %@ / %@", [NSValue valueWithRange:substringRange], [NSValue valueWithRange:enclosingRange]);
}
}];
除錯控制臺輸出:
proj[64417:6900139] found at: NSRange: {3, 5} / NSRange: {3, 6}
proj[64417:6900139] found at: NSRange: {13, 5} / NSRange: {13, 5}
編輯
如果要查找以字串開頭的所有單詞#,例如:
"this #is a #test of #88 including embedded #tags such as Mr.#tags. #Is that #what you're looking #for?"
與查找多次出現的單詞(如您的問題)相反,您可能希望使用正則運算式。
另一個快速、簡單的例子:
NSString *searchedString = @"this #is a #test of #88 including embedded #tags such as Mr.#tags. #Is that #what you're looking #for?";
NSRange searchedRange = NSMakeRange(0, [searchedString length]);
// searching for any Word that begins with #
NSString *pattern = @"\\#\\w ";
NSError *error = nil;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error];
if (error) {
NSLog(@"Could not create regex!!!");
return;
}
NSArray *matches = [regex matchesInString:searchedString options:0 range: searchedRange];
for (NSTextCheckingResult *match in matches) {
NSString *matchText = [searchedString substringWithRange:[match range]];
NSLog(@"match: %@", matchText);
}
除錯控制臺輸出:
proj[66898:6935236] match: #is
proj[66898:6935236] match: #test
proj[66898:6935236] match: #88
proj[66898:6935236] match: #tags
proj[66898:6935236] match: #tags
proj[66898:6935236] match: #Is
proj[66898:6935236] match: #what
proj[66898:6935236] match: #for
編輯 2
您的第二條評論確實應該是一篇新文章,因為這是一個完全不同的問題(關于 RegEx 語法)。
但是,為了讓你上路...
NSString *searchedString = @"These #tags, with #various& #excluded! #chars. And #other@ formats #88 including #emo??jis or #?????????? even middle#tags";
NSRange searchedRange = NSMakeRange(0, [searchedString length]);
// searching for any Sub-String that
// begins with #
// ends with a character from an "end" set
//
// Notes:
// \s means any white-space
// \ needs to be escaped, e.g. \\ or \\s
// you probably want to add more "end" chars
NSString *endChars = @"\\s\\?!@;:%^&*().,";
NSString *pattern = [NSString stringWithFormat:@"\\#[^%@] ", endChars];
NSError *error = nil;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error];
if (error) {
NSLog(@"Could not create regex!!!");
return;
}
NSArray *matches = [regex matchesInString:searchedString options:0 range: searchedRange];
for (NSTextCheckingResult *match in matches) {
NSString *matchText = [searchedString substringWithRange:[match range]];
NSLog(@"match: %@", matchText);
}
除錯控制臺輸出:
proj[73128:7009059] match: #tags
proj[73128:7009059] match: #various
proj[73128:7009059] match: #excluded
proj[73128:7009059] match: #chars
proj[73128:7009059] match: #other
proj[73128:7009059] match: #88
proj[73128:7009059] match: #emo??jis
proj[73128:7009059] match: #??????????
proj[73128:7009059] match: #tags
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/426048.html
上一篇:從字串資料型別到影像的影像轉換
