iOS swift 字符串(String)基础 用法

124 阅读2分钟

ios swift 基础 合集- 荔枝 点击查看

image.png

  • 声明String, 字符串字面量(String Literals)

let someString = "Some string literal value"
  • 字符串插值 (String Interpolation)

let multiplier = 3 
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// message is "3 times 2.5 is 7.5"
  • 计算字符数量 (Counting Characters) .count

let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪" 
print("unusualMenagerie has \(unusualMenagerie.count) characters") 
// Prints "unusualMenagerie has 40 characters"
let greeting = "Guten Tag!" 
greeting[greeting.startIndex] 
// G 
greeting[greeting.index(before: greeting.endIndex)] 
// ! 
greeting[greeting.index(after: greeting.startIndex)] 
// u 
let index = greeting.index(greeting.startIndex, offsetBy: 7) 
greeting[index] 
// a
for index in greeting.indices { 
  print("\(greeting[index]) ", terminator: "") 
} 
// Prints "G u t e n T a g ! "
  • String截取

let greeting = "Hi there! It's nice to meet you! 👋"
if let endOfSentence = greeting.firstIndex(of: "!"){
    let firstSentence = greeting[...endOfSentence]
    // firstSentence == "Hi there!"
    let three = greeting[..<endOfSentence]
    // three == "Hi there"
}
var snowy = "Let it snow!"
var newSnow = snowy.prefix(3)
//newSnow = "Let"
var snowy = "Let it snow!"
var newSnow = snowy.prefix(upTo: snowy.index(snowy.startIndex, offsetBy: 3))
//newSnow = "Let"
var snowy = "Let it snow!"
let newSnowy =  snowy.suffix(3)
//newSnowy = "ow!"
var snowy = "Let it snow!"
if let i = snowy.firstIndex(of: "i"){
    let newSnowy =  snowy.suffix(from: i)
    //newSnowy  = "it snow!"
}
  • String 拼接

let greeting = "Hi there! It's nice to meet you! "
let name = "Li Zhi"
let longGreeting = greeting + name
//longGreeting = "Hi there! It's nice to meet you! Li Zhi"
var greeting = "Hi there! It's nice to meet you! "
let name = "Li Zhi"
greeting += name
//greeting = "Hi there! It's nice to meet you! Li Zhi"
var greeting = "Hi there! It's nice to meet you! "
let name = "Li Zhi"
greeting.append(name)
//greeting = "Hi there! It's nice to meet you! Li Zhi"
var greeting = "Hi there! It's nice to meet you! "
let name = "Li Zhi"
greeting.append(contentsOf: name)
//greeting = "Hi there! It's nice to meet you! Li Zhi"
  • 插入

var welcome = "hello" 
welcome.insert("!", at: welcome.endIndex)
// welcome now equals "hello!" 
welcome.insert(contentsOf: " there", at: welcome.index(before: welcome.endIndex)) 
// welcome now equals "hello there!"

-#### String 删除

welcome.remove(at: welcome.index(before: welcome.endIndex)) 
// welcome now equals "hello there" 
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex 
welcome.removeSubrange(range) // welcome now equals "hello"
var greeting = "Hi there! It's nice to meet you! "
greeting.remove(at: greeting.index(greeting.startIndex, offsetBy: 2))
//greeting = "Hithere! It's nice to meet you! "
var snowy = "Let it snow!"
snowy.dropLast(3)
var greeting = "Hi there! It's nice to meet you! "
greeting.removeAll()
//greeting = ""
var greeting = "Hi there! It's nice to meet you! "
greeting.removeFirst(2)
  • suffix词尾,prefix开头

var snowy = "Let it snow!"
snowy.suffix(3)
var snowy = "Let it snow!"
snowy.prefix(3)
var greeting = "Guten Tag"
greeting.hasPrefix("123")
greeting.hasSuffix("321")
  • 替换

var greeting:String = "Hi there! It's nice to meet you! "
greeting.replacing("Hi", with: "Hello")
var greeting:String = "Hi there! It's nice to meet you! "

let startIndex = greeting.startIndex

let endIndex = greeting.index(greeting.startIndex, offsetBy: 2)

greeting.replaceSubrange(startIndex..<endIndex, with: "Hello")
  • String 内 Range 使用

let snowy = "❄️ Let it snow! ☃️"
let nsrange = NSRange(location: 3, length: 12)
if let range = Range(nsrange, in: snowy) {
    print(snowy[range])
}
  • contains

let lowercase = "a"..."z"
 print(lowercase.contains("z"))
  • String 分割字符串

let nameStr = "John,Andy,Nancy"
let names = nameStr.split(separator: ",")
//names = ["John", "Andy", "Nancy"]
  • String ,Array 继承 Collection 区分

StringArray协议.png