swift 字串拼接、截取

來源:互聯網
上載者:User
import UIKit//swift 字串//1>swift中的字串String是一個結構體,效能更高//2>NSString是一個OC對象,效能略差//3>String支援直接遍曆//4>Swift提供了String和NSString的無縫轉換let str = "hello world"//遍曆字串for c in str.characters{    print(c)}//字串拼接//兩個字串之間的拼接let str1 = "你好"let str2 = "世界"let str3 = str1 + str2//字串和其它標識符的拼接let name = "ZYF"let age = 18let height = 1.88let info = "my name is \(name) , my age is \(age) , my height is \(height)"//拼接字串時,字串的格式化let minent = 2let seconds = 8let timeString = String(format: "%02d:%02d", arguments:[minent,seconds])print(timeString)//字串的截取let urlStr = "www.zhengyanfeng.com"//將String類型轉換為NSString類型let header = (urlStr as NSString).substring(to: 3)let body = (urlStr as NSString).substring(with: NSRange(location: 4, length: 12))let afterbody = (urlStr as NSString).substring(from: 17)

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.