Forked from mortenjust/get word at a given position in a string.swift
Created
December 5, 2015 04:36
-
-
Save Bostonncity/1875906a35de930ccb39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func wordRangeAtIndex(index:Int, inString str:NSString) -> NSRange { | |
let tagger = NSLinguisticTagger(tagSchemes: [NSLinguisticTagSchemeTokenType], options: 0) | |
var r : NSRange = NSMakeRange(0,0) | |
tagger.string = str as String | |
tagger.tagAtIndex(index, scheme: NSLinguisticTagSchemeTokenType, tokenRange: &r, sentenceRange: nil ) | |
return r | |
} | |
func wordAtIndex(index:Int, inString str:NSString) -> NSString { | |
return str.substringWithRange(wordRangeAtIndex(index, inString: str)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yuuu