moo*_*ots 5 cocoa substring nsstring ios
我想从给定索引处的NSString中提取子字符串.例:
NSString = @"Hello, welcome to the jungle";
int index = 9;
Run Code Online (Sandbox Code Playgroud)
索引点'9'位于'welcome'一词的中间,我希望能够将'welcome'这个词作为子串提取出来.谁能告诉我如何实现这一目标?正则表达式?
这是NSString上的一个类别的解决方案:
- (NSString *) wordAtIndex:(NSInteger) index {
__block NSString *result = nil;
[self enumerateSubstringsInRange:NSMakeRange(0, self.length)
options:NSStringEnumerationByWords
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if (NSLocationInRange(index, enclosingRange)) {
result = substring;
*stop = YES;
}
}];
return result;
}
Run Code Online (Sandbox Code Playgroud)
而另一个,更复杂,但让你准确指定你想要的单词字符:
- (NSString *) wordAtIndex:(NSInteger) index {
if (index < 0 || index >= self.length)
[NSException raise:NSInvalidArgumentException
format:@"Index out of range"];
// This definition considers all punctuation as word characters, but you
// can define the set exactly how you like
NSCharacterSet *wordCharacterSet =
[[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet];
// 1. If [self characterAtIndex:index] is not a word character, find
// the previous word. If there is no previous word, find the next word.
// If there are no words at all, return nil.
NSInteger adjustedIndex = index;
while (adjustedIndex < self.length &&
![wordCharacterSet characterIsMember:
[self characterAtIndex:adjustedIndex]])
++adjustedIndex;
if (adjustedIndex == self.length) {
do
--adjustedIndex;
while (adjustedIndex >= 0 &&
![wordCharacterSet characterIsMember:
[self characterAtIndex:adjustedIndex]]);
if (adjustedIndex == -1)
return nil;
}
// 2. Starting at adjustedIndex which is a word character, find the
// beginning and end of the word
NSInteger beforeBeginning = adjustedIndex;
while (beforeBeginning >= 0 &&
[wordCharacterSet characterIsMember:
[self characterAtIndex:beforeBeginning]])
--beforeBeginning;
NSInteger afterEnd = adjustedIndex;
while (afterEnd < self.length &&
[wordCharacterSet characterIsMember:
[self characterAtIndex:afterEnd]])
++afterEnd;
NSRange range = NSMakeRange(beforeBeginning + 1,
afterEnd - beforeBeginning - 1);
return [self substringWithRange:range];
}
Run Code Online (Sandbox Code Playgroud)
假设单词很短,第二个版本对于长字符串也更有效.