工具扩展_3_EZSwiftExtensions_string

该文详细介绍了如何在Swift中进行字符串到数字、Bool类型的转换,并展示了字符串的修剪、截取、检测等操作。此外,还讨论了富文本的创建和属性设置,包括字体样式、颜色等。
摘要由CSDN通过智能技术生成
    1. 字符串的扩展:
    1. 富文本 :请添加图片描述
    1. 字符串的扩展:
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        yeTyeString()
    }
    
    func yeTyeString(){
        ///--  1.字符串 转 数字 / Bool 类型:
        var myString = "33.3"
        print("---1. 字符串转成双精度,myString.toDouble:\(String(describing: myString.toDouble))")
        print("---2. 字符串转成整形,myString.toInt:\(String(describing: myString.toInt))")
        
        let myNumberString = "13"
        print("---3. 将字符串转成int类型,myNumberString.toInt():\(String(describing: myNumberString.toInt()))") //Optional(13)
        print("---4. 将字符串转成Doubel类型,myNumberString.toDouble():\(String(describing: myNumberString.toDouble()))") //Optional(13.0)
        print("---5. 将字符串转成Float类型,myNumberString.toFloat():\(String(describing: myNumberString.toFloat()))") //Optional(13.0)
        
        let myBoolString = "false"
        let myOtherString = "hello"
        print("---6. 将字符串转成Bool类型, myBoolString.toBool():\(String(describing: myBoolString.toBool()))") // Optional(false)
        print("---7. 将字符串转成Bool类型,myOtherString.toBool():\(String(describing: myOtherString.toBool()))") //nil
        
        ///-- 2.
        myString.trim()
        print("---8. 删除空格和换行符,myString:\(myString)")
        
        let eZSwiftExtensions = "eZSwiftExtensions"
        print("---9. 输出指定索引位置的字符,eZSwiftExtensions[2]:\(eZSwiftExtensions[2])") //S;
        print("---10. 输出指定索引位置的字符,eZSwiftExtensions[3]:\(eZSwiftExtensions[3])") //w;
        print("---11. 输出指定索引位置范围的字符,eZSwiftExtensions[2...4]:\(eZSwiftExtensions[2...4])") // Swi;
        print("---12. 字符在字符串中的位置索引,eZSwiftExtensions.getIndexOf(w):\(eZSwiftExtensions.getIndexOf("w") ?? 0)") //3;
        
        let awesomeString = "eZSwiftExtensions is awesome!"
        print("---13.字符串的长度,awesomeString.length:\(awesomeString.length)")//29
        print("---14.将每个单词的首字母都大写,awesomeString.capitalized:\(awesomeString.capitalized)") //Ezswiftextensions Is Awesome!
        
        let awesomeString2 = "eZSwiftExtensions is awesome!"
        print("---15.是否包含另一个字符串, awesomeString2.contains(squirtle):\(awesomeString2.contains("squirtle"))") //false
        
        print("---16.设置在检测是否包含另一个字符串时,是否需要区分大小写: (不区分大小写)");
        print(awesomeString2.contains("AWESOME", compareOption: NSString.CompareOptions.caseInsensitive)) //true
        
        let awesomeString3 = "\n    eZSwiftExtensions is awesome!     \n \n "
        let emptyStr = "   \n \n \n"
        
        print("---17.检测字符串是否为空或只包含空格、换行符,awesomeString3.isBlank:\(awesomeString3.isBlank)") // false
        print("---18.检测字符串是否为空或只包含空格、换行符,emptyStr.isBlank:\(emptyStr.isBlank)") //true;
        
        let awesomeString4 = "eZSwiftExtensions is awesome!"
        let emailStr = "charmander@gmail.com"
        print("---19.检测字符串是否为邮箱格式,awesomeString4.isEmail:\(awesomeString4.isEmail)") //false;
        print("---20.检测字符串是否为邮箱格式,emailStr.isEmail:\(emailStr.isEmail)") //true;
        
        let urlString = "http://www.google.com is great but www.xxx.com not that much"
        print("---21.通过抽取网址列表方法,抽取字符串中的所有网址, myString.extractURLs:\(myString.extractURLs)") // []
        print("---22.抽取字符串中的所有网址,emailStr.extractURLs:\(emailStr.extractURLs)") //[mailto:charmander@gmail.com]
        print("---23.抽取字符串中的所有网址,urlString.extractURLs:\(urlString.extractURLs)") //[http://www.google.com, http://www.xxx.com]
        
        ///-- 3.
        let myStr = "10.5"
        let myOtherStr = "Legolas"
        print("---24.字符串是否完全由数字组成,myStr.isNumber():\(myStr.isNumber())") //true;
        print("---25.字符串是否完全由数字组成,myOtherStr.isNumber():\(myOtherStr.isNumber())") //false;
        
        let str = "Like all Apple Watch Series 2 models, Apple Watch Nike+ has built-in GPS to track your pace, distance, and route — even if you don’t have your iPhone with you. With the brightest display Apple has ever made, your metrics are easy to read, no matter how much the sun glares. And Apple Watch Nike+ is rated water resistant 50 meters,* so you can even take a post-run dip in the pool."
        print("---26. 统计子在字符串中总共包含指定字符串的数量,str.count:\(str.count("Watch"))") //3;
    }
}
    1. 富文本:
    ///-- 富文本:
    func yeTyeNSAttributedString(){
        let str = NSAttributedString(string: "Like all Apple Watch Series 2 models, Apple Watch Nike+ has built-in GPS to track your pace, distance, and route — even if you don’t have your iPhone with you. With the brightest display Apple has ever made, your metrics are easy to read, no matter how much the sun glares. ")
        
        // 添加下划线, 加粗, 设置颜色, 设置这些属性的应用范围:
        var attrStr1 = str.underline().bold().color(.orange).attributedSubstring(from: NSRange(location: 0, length: 120))
        
        // 设置样式,斜体, 设置这些属性的应用范围:
        let attrStr2 = str.color(.purple).italic().attributedSubstring(from: NSRange(location: 120, length: str.length - 121))
        
        attrStr1 += attrStr2
        let label = UILabel(frame: CGRect(x: 20, y: 40, width: 374, height: 300))
        label.numberOfLines = 0
        label.attributedText = attrStr1
        self.view.addSubview(label)
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值