★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ )
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10499867.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Scanner字符串扫描类:
1.Scanner继承自NSObject,遵守NSCopying协议。是一个用于扫描指定字符串的抽象类。
2.可以创建Scanner时制定他的String属性,然后scanner会按照你的要求从头到尾扫描这个字符串的每个字符。
3.扫描操作从上次扫描的位置开始,并且继续往后扫描直到指定的内容出现为止(如果有的话)。
一、部分属性详解
1 //(只读)返回当前被扫描的字符串 2 open var string: String { get } 3 4 //当前扫描的位置,默认是从0开始的。通过设置这个属性值,可以自定义扫描的位置 5 open var scanLocation: Int 6 7 //这个集合是用来设置我们需要跳过扫描的一个字符集合 8 open var charactersToBeSkipped: CharacterSet? 9 10 //是否大小写敏感 11 open var caseSensitive: Bool 12 13 //用给定扫描字符串初始化 14 public init(string: String)
二、部分方法详解
2.10、方法内容:
(1)扫描该类型的字符串,并将扫描结果赋值给result
(2)判断该字符串的第一个字符(默认第一个,可根据scanLocation属性设置)是否为该类型
1 open func scanInt32(_ result: UnsafeMutablePointer<Int32>?) -> Bool 2 3 @available(iOS 2.0, *) 4 open func scanInt(_ result: UnsafeMutablePointer<Int>?) -> Bool 5 6 open func scanInt64(_ result: UnsafeMutablePointer<Int64>?) -> Bool 7 8 @available(iOS 7.0, *) 9 open func scanUnsignedLongLong(_ result: UnsafeMutablePointer<UInt64>?) -> Bool 10 11 open func scanFloat(_ result: UnsafeMutablePointer<Float>?) -> Bool 12 13 open func scanDouble(_ result: UnsafeMutablePointer<Double>?) -> Bool
2.11、示例:只包含数字的字符串
1 var intType:Int = 0 2 var int64Type:Int64 = 0 3 var int32Type:Int32 = 0 4 var floatType:Float = 0.0 5 var doubleType:Double = 0.0 6 //判断字符串包含的类型(输出结果为true) 7 print(Scanner(string: "123456789").scanInt(&intType)) 8 print(Scanner(string: "123456789").scanInt64(&int64Type)) 9 print(Scanner(string: "123456789").scanInt32(&int32Type)) 10 print(Scanner(string: "123456789.99").scanFloat(&floatType)) 11 print(Scanner(string: "123456789.99").scanDouble(&doubleType)) 12 //Print 13 /* 14 true 15 true 16 true 17 true 18 true 19 */
2.12、示例:包含非数字的字符串
1 var intType: Int = 0 2 let scanner = Scanner(string: "a1b2c3d4") 3 scanner.scanLocation = 1 4 print(scanner.scanInt(&intType)) 5 //Print true 6 7 var doubleType: Double = 0.0 8 let scanner1 = Scanner(string: "a1b2.5c3d4") 9 scanner1.scanLocation = 0 10 print(scanner1.scanDouble(&doubleType)) 11 //Print false
2.20、方法内容:
(1)扫描字符串前缀是否是"0x"或者"0X",返回true或false.
(2)将0x(0X)字符后面符合十六进制数的字符串转化成十进制数(可运用到UIColor关于十六进制数的转化)。
1 // Optionally prefixed with "0x" or "0X" 2 open func scanHexInt32(_ result: UnsafeMutablePointer<UInt32>?) -> Bool 3 4 @available(iOS 2.0, *) 5 // Optionally prefixed with "0x" or "0X" 6 open func scanHexInt64(_ result: UnsafeMutablePointer<UInt64>?) -> Bool 7 8 @available(iOS 2.0, *) 9 // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix. 10 open func scanHexFloat(_ result: UnsafeMutablePointer<Float>?) -> Bool 11 12 @available(iOS 2.0, *) 13 // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix. 14 open func scanHexDouble(_ result: UnsafeMutablePointer<Double>?) -> Bool
2.21、示例:
1 var uint32Type:UInt32 = 0 2 var uint64Type:UInt64 = 0 3 4 print(Scanner(string: "0xab").scanHexInt32(&uint32Type)) 5 //Print true 6 print(uint32Type) 7 //Print 171 8 print(Scanner(string: "xcd").scanHexInt64(&uint64Type)) 9 //Print false 10 print(Scanner(string: "0xcd").scanHexInt64(&uint64Type)) 11 //Print true 12 print(uint64Type) 13 //Print 205
2.30、从字符串中找出给定字符的所在位置
open func scanString(_ string: String, into result: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool
2.31、示例代码
1 //声明一个字符串变量,为可选类型 2 var str:String? = String() 3 //初始化一个字符串扫描对象 4 let scan = Scanner(string: "strengthen") 5 //判断是否扫描到字符串的末尾 6 while !scan.isAtEnd 7 { 8 //扫描位置加1 9 scan.scanLocation += 1 10 //输出扫描位置 11 print(scan.scanLocation) 12 //从字符串中找出给定字符的所在位置 13 scan.scanString("n", into: &str) 14 //输出字符串,注意str为可选类型 15 print(str) 16 //重新初始化 17 str = String() 18 } 19 //Print 20 /* 21 1 22 Optional("") 23 2 24 Optional("") 25 3 26 Optional("") 27 4 28 Optional("n") 29 6 30 Optional("") 31 7 32 Optional("") 33 8 34 Optional("") 35 9 36 Optional("n") 37 */
2.4、Scanner的其他方法
1 //从scan中扫描出string放入result中,如果不需要存储string,则result传NULL 2 open func scanCharacters(from set: CharacterSet, into result: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool 3 4 //从scan中扫描出string之外的数据放入result中,如果不需要存储string,则result传NULL 5 open func scanUpTo(_ string: String, into result: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool 6 7 //从scan中扫描出set之外的数据放入result中,如果不需要存储string,则result传NULL 8 open func scanUpToCharacters(from set: CharacterSet, into result: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool
2.5、使用Scanner处理UIColor十六进制数值:在UIColor的extension中添加扩展方法
1 /* 十六进制颜色扩展*/ 2 convenience init?(hex : String, alpha : CGFloat = 1.0) { 3 //1.判断字符串长度是否符合 4 guard hex.characters.count >= 6 else { 5 return nil 6 } 7 //2.将字符串转成大写 8 var tempHex = hex.uppercased() 9 //3.判断开头 10 if tempHex.hasPrefix("0x") || tempHex.hasPrefix("##") || tempHex.hasPrefix("0X") { 11 //去掉开头 12 tempHex = tempHex.dropFirst(2) 13 } 14 if tempHex.hasPrefix("#") { 15 tempHex = tempHex.dropFirst() 16 } 17 //4.分别截取RGB 18 var range = NSRange(location: 0, length: 2) 19 let rHex = (tempHex as NSString).substring(with: range) 20 range.location = 2 21 let gHex = (tempHex as NSString).substring(with: range) 22 range.location = 4 23 let bHex = (tempHex as NSString).substring(with: range) 24 //5.将字符串转化成数字 emoji也是十六进制表示(此处也可用Float类型) 25 var r : UInt32 = 0, g : UInt32 = 0, b : UInt32 = 0 26 //创建扫描器,将字符串的扫描结果赋值给:r,g,b 27 Scanner(string: rHex).scanHexInt32(&r) 28 Scanner(string: gHex).scanHexInt32(&g) 29 Scanner(string: bHex).scanHexInt32(&b) 30 31 self.init(r : CGFloat(r), g : CGFloat(g), b : CGFloat(b)) 32 } 33 34 /*RGB三原色*/ 35 convenience init(r : CGFloat, g : CGFloat, b : CGFloat, alpha : CGFloat = 1.0) { 36 self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: alpha) 37 }
请发表评论