Swift学习 字符串的操作

前端之家收集整理的这篇文章主要介绍了Swift学习 字符串的操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、字符串的拼接

字符串的拼接用 “+”

let str1 = "Hello"
   let str2 = "World"
   let str3 = str1 + str2
   print(str3);

输出:HelloWorld

2、字符串的插入

字符串的插入用 “\( )”

for var i = 0; i<3; i++ {
    print("index is \(i)")

输出
index is 0
index is 1
index is 2

3、字符串的遍历

let str = "This is a sting"
    for character in str.characters {
        print(character)
    }

4、字符串的长度

let str = "This is a sting"
    let num = str.characters.count;
    print(num)

输出:15

5、字符串的判断

//判断是否相等
    let str1 = "This is a sting"
    let str2 = "This"
    print(str1 == str2)

    //判断是否包含前缀
    print(str1.hasPrefix(str2))

    //判断是否包含后缀
    print(str1.hasSuffix(str2))

6、字符串分割成数组

let date = "2016-03-10"
    let arr = date.componentsSeparatedByString("-");
    for str in arr {
        print(str)
    }
原文链接:https://www.f2er.com/swift/324351.html

猜你在找的Swift相关文章