我如何在 objective c 的句子中找到单词的位置范围?
How do i find range of locations of a word in a sentence in objective c?
如何在 objective c 的句子中查找特定单词的位置范围?
假设我有以下 NSString
NSString *stringOfHashTags = "mr#hello Mr.#hello"
我正在使用
NSString word=@"hello";
NSRange termsRange = [stringOfHashTags rangeOfString:[word substringFromIndex:[word rangeOfString:@"#"].location]];
查找以 # 开头的单词的位置,但如果该单词在句子中出现两次或多次,它只会 returns 第一个位置。任何帮助表示赞赏。
您可以使用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
您的第二条评论确实应该是新的 post,因为这是一个完全不同的问题(关于 RegEx 语法)。
但是,为了让您上路...
NSString *searchedString = @"These #tags, with #various& #excluded! #chars. And #other@ formats #88 including #emojis 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: #emojis
proj[73128:7009059] match: #
proj[73128:7009059] match: #tags
如何在 objective c 的句子中查找特定单词的位置范围?
假设我有以下 NSString
NSString *stringOfHashTags = "mr#hello Mr.#hello"
我正在使用
NSString word=@"hello";
NSRange termsRange = [stringOfHashTags rangeOfString:[word substringFromIndex:[word rangeOfString:@"#"].location]];
查找以 # 开头的单词的位置,但如果该单词在句子中出现两次或多次,它只会 returns 第一个位置。任何帮助表示赞赏。
您可以使用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
您的第二条评论确实应该是新的 post,因为这是一个完全不同的问题(关于 RegEx 语法)。
但是,为了让您上路...
NSString *searchedString = @"These #tags, with #various& #excluded! #chars. And #other@ formats #88 including #emojis 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: #emojis
proj[73128:7009059] match: #
proj[73128:7009059] match: #tags