天天看點

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