天天看点

Swift - 14 - 字符串的基础操作

//: Playground - noun: a place where people can play

import UIKit

// 拼接
var str = "Hello, playground"
str + "hello, swift"    // 这样的拼接, str还是没有改变
str
str += "hello, swift"
str

// 比较
var c1 = "abc"
var c2 = "abd"
c1 < c2

var c3 = "abbbb"
c1 > c3

// 前缀和后缀
var chapter = [
    "第一章: 1.为你的下一代iOS应用开发做准备",
    "第二章: 1.使用xcode7",
    "第二章: 2.常量和变量",
    "第二章: 3.布尔类型及if语句",
    "第二章: 4.元组"
]

var count = 0
for name in chapter {
    if (name.hasPrefix("第二章")) {
        count++
    }
}
print("第二章有\(count)小节")
      

  

转载于:https://www.cnblogs.com/Rinpe/p/5050786.html