iOS swift字符串、数组

2021-02-22  本文已影响0人  我不白先生
//显式字符串写法
var str = "Hello, playground"
//隐式字符串
var s:String = "abcd"
//字符串拼接
s = "打算发的说法是\(18) s=\(s)"
var s2 = s + str
s2 += "a"

//判断相等
s == s2
s.hasPrefix("a")
s2.hasSuffix("a")

//判断是否是空字符串
s = ""
s.isEmpty

//分割字符串
s = "a,b,c,d"
s.components(separatedBy: ",")

//数组
//隐式
let arr = [1,2,3,4]
//显式
let arr2:[Int] = [3,4,1,3]

let arr3:[String] = ["a", "b", "c"]

//初始化可变数组
//声明一个可变的数组
var arr4:[String] = ["a", "b", "c"]
arr4.append("d")
//编历数组
for item in arr4 {
    print(item)
}
for i in 0..<arr4.count {
    print("第\(i)个是\(arr4[i])")
}
//迭代的方式遍历数组
//for (i,item) in arr4.enumerated(){
//    print("第\(i)个是\(item)")
//}
//数组的属性
arr4.count
arr4.min()
arr.max()
arr.first
arr.last

//获取数组中数据
arr[0]
arr[0...2]

//数组拼接
var arr5 = arr+arr2

//修改数组中数据
arr4[0] = "m"
arr4[0...2] = ["x", "y", "z"]
//删除
arr4.remove(at: 0)
arr4
arr4.removeLast()
arr4.removeFirst()
上一篇下一篇

猜你喜欢

热点阅读