importUIKitclassViewController:UIViewController{overridefuncviewDidLoad(){super.viewDidLoad()yeTyeString()}funcyeTyeString(){///-- 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)")//29print("---14.将每个单词的首字母都大写,awesomeString.capitalized:\(awesomeString.capitalized)")//Ezswiftextensions Is Awesome!let awesomeString2 ="eZSwiftExtensions is awesome!"print("---15.是否包含另一个字符串, awesomeString2.contains(squirtle):\(awesomeString2.contains("squirtle"))")//falseprint("---16.设置在检测是否包含另一个字符串时,是否需要区分大小写: (不区分大小写)");print(awesomeString2.contains("AWESOME", compareOption:NSString.CompareOptions.caseInsensitive))//truelet awesomeString3 ="\n eZSwiftExtensions is awesome! \n \n "let emptyStr =" \n \n \n"print("---17.检测字符串是否为空或只包含空格、换行符,awesomeString3.isBlank:\(awesomeString3.isBlank)")// falseprint("---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;}}
富文本:
///-- 富文本:funcyeTyeNSAttributedString(){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)}