IOS SDK detailed nsscanner-parsing string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.