標籤:eval cas ack == uikit result swift開發 fun 字元
//
// ViewController.swift
// Swift+String
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//var 變數 let常量 就不過多敘述了
self.view.backgroundColor = UIColor.yellow;
let my:NSString = "My name"
let name = " is 輝哥"
var myName = ""
let myfirst:NSString = "我的 名字"
myName = (my as String) + name
print(myName)
//計算字串的長度
print(myfirst.length)//nsstring類型字串
print(myName.characters.count)//string類型字串
//擷取字串位置
var strA = "中國是最強大的國家"
let startindex = strA.startIndex//擷取起始位置
print(startindex)
let endIndex = strA.endIndex//擷取終止位置
print(endIndex)
let char = strA[strA.index(after: startindex)]//擷取某個下標後一個下標對應的字元
print(char)
let char2 = strA[strA.index(before: endIndex)]//擷取某個下標前一個下標對應的字元
print(char2)
let subString = strA[startindex...strA.index(startindex, offsetBy: 4)]//通過範圍擷取字串中的某一個子串
print(subString)
let num = strA.range(of: "最強大")//擷取某字串在父串中的範圍
print(num)
strA.append(" 世界上的")//追加字串操作
print(strA)
strA.insert("~", at: strA.index(strA.startIndex, offsetBy: 10))//在指定位置出阿如一組字元
print(strA)
strA.replaceSubrange(strA.startIndex...strA.index(strA.startIndex, offsetBy: 9), with: "我們大中國永遠屹立在")//在指定範圍替換一個字串
print(strA)
strA.remove(at: strA.index(before: strA.endIndex))//在指定位置刪除一個字元
print(strA)
strA.removeSubrange(strA.startIndex...strA.index(strA.startIndex, offsetBy: 2))
print(strA)
var strB = "myname"
strB = strB.uppercased()//全部轉為大寫
print(strB)
strB = strB.lowercased()//全部轉為小寫
print(strB)
//字串的串連
//string 型字串
let str1:String = "上"
let str2:String = "海"
//使用" + "號串連兩個字串
var str3:String = str1 + str2
print(str3)
//在字串的末尾拼接字串,代替str3.extend("123456789")
str3.write("123456789")
print(str3)
//字串比較
//sting型字串
let str4:String = "SHANGHAI"
let str5:String = "shanghai"
//判斷連個字串是否相等
let bl1:Bool = str4 == str5
print(bl1)
//判斷字串是否為空白
let bl2:Bool = str4.isEmpty
print(bl2)
//判斷字串首碼
let bl3:Bool = str4.hasPrefix("SHANG")
print(bl3)
//判斷字串尾碼
let bl4:Bool = str4.hasSuffix("HAI")
print(bl4)
//比較兩個字串
let result1:ComparisonResult = str4.compare(str5)
print(result1)
//忽略大小寫比較
let result2:ComparisonResult = str4.caseInsensitiveCompare(str5)
print(result2)
//NSSting 型字串
let str6:NSString = "shanghai"
let str7:NSString = "SHANGHAI"
//判斷兩個字串是否相等
let bl5:Bool = str6.isEqual(str7)
print(bl5)
//判斷字串首碼
let bl6:Bool = str6.hasPrefix("shang")
print(bl6)
//判斷字串尾碼
let bl7:Bool = str6.hasSuffix("hai")
print(bl7)
//忽略大小寫比較
let result3:ComparisonResult = str6.compare(str7 as String)
print(result3)
let result4:ComparisonResult = str6.caseInsensitiveCompare(str7 as String)
print(result4)
//字串切割
//Sting 型字串
let str11:String = "dong$fang$ming$zhu"
//字串 按照字元切割
let arr1:Array = str11.components(separatedBy: "$")
print(arr1)
//自動按照" / "分割字串 NSString特有,string沒有
let str13:NSString = "Users/JHQ0228/Desktop"
let arr3:Array = str13.pathComponents
print(arr3)
//字串與數位相互轉換 有一些用的比較少,就當做瞭解一下吧
// String 型字串
// 字元 轉 數字
let charr:Character = "A";
var charVal1:Int = 0
// 遍曆字元轉換為的字串
for charStr in String(charr).unicodeScalars {
// 將取出的字元轉換為 Int 型數字
charVal1 = Int(charStr.value)
}
// 字串 轉 數字
// 數字型字串 轉 Int 型數字,代替 intStr.toInt()
let intStr:String = "64"; let intVal1:Int? = Int(intStr)
// 數字 轉 字元
let intVal2 = 65; let charVal2:Character = Character(UnicodeScalar(intVal2)!)
// 數字 轉 字串
// Int 型數字 轉 字串
let intVal3:Int = 123; let intStr1:String = intVal3.description
// Int32 型數字 轉 字串
let int32Val1:Int32 = 32; let int32Str1:String = int32Val1.description
// Int64 型數字 轉 字串
let int64Val1:Int64 = 64; let int64Str1:String = int64Val1.description
// Float 型數字轉 字串
let floatVal1:Float = 2.1; let floatStr1:String = floatVal1.description
// Double 型數字轉 字串
let doubleVal1:Double = 162.1; let doubleStr1:String = doubleVal1.description
// Bool 型 轉 字串
let boolVal1:Bool = true; let boolStr1:String = boolVal1.description
// NSString 型字串
// 字串 轉 數字
// 字串 轉 Int 型數字
let intVal4:Int = NSString(string: "6432").integerValue
// 字串 轉 Int32 型數字
let int32Val2:Int32 = NSString(string: "32").intValue
// 字串 轉 Int64 型數字
let int64Val2:Int64 = NSString(string: "64").longLongValue
// 字串 轉 Float 型數字
let floatVal2:Float = NSString(string: "2.1").floatValue
// 字串 轉 Double 型數字
let doubleVal2:Double = NSString(string: "162.1").doubleValue
// 字串 轉 Bool 型
let boolVal2:Bool = NSString(string: "false").boolValue
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Swift開發String+NSString詳細使用