Original blog, reproduced please indicate the source
Blog.csdn.net/hello_hwc
Welcome to my iOS SDK detailed column, where there are a number of basic articles
Http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html
Preface: Nsscanner is a good tool for parsing string, converting string to substring and numbers. It uses a nsstring initialization, which is usually scanned from the beginning to the end when used.
This article will first give two examples, and then explain in detail the method of Nsscanner. Source code is the swift version, can be copied directly into the playground execution.
Example One
Parsing a fixed-format string
//format is this-csdn://[user: Name]~[url: Link]~[id: identifier]varstr ="csdn://[user:wecnhenhuang]~[url:blog.csdn.net]~[id:123456]"varScanner = Nsscanner (string: STR) scanner.scanstring ("Csdn://[user:", intostring:Nil)varName:nsstring? =NilvarUrl:nsstring? =NilvarId:nsstring? =NilScanner.scanuptostring ("]~[url:", intostring: &name) scanner.scanstring ("]~[", intostring:Nil) Scanner.scanuptostring ("]~[", intostring: &url) scanner.scanstring ("]~[id:", intostring:Nil) Scanner.scanuptostring ("]", intostring: &id)
After analysis,
Example Two
Parsing a hexadecimal string
"0x11ff"var scanner = NSScanner(string2var0scanner.scanHexInt(&a)
Results
a//4607
Method Example
caseSensitive // 忽略大小写charactersToBeSkipped // 忽略某些字符集locale // 地区- scanCharactersFromSet:intoString: // 把charSet中的扫描到string 中- scanUpToCharactersFromSet:intoString://直到扫描到chartset,把扫描到的存储到String中- scanDecimal: // 扫描十进制- scanHexInt:// 扫描十六进制到Int里
More documentation
Https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSScanner_ Class/index.html
IOS SDK detailed nsscanner-parsing string