标签:
String
的原因
String
是一个结构体,性能更高NSString
是一个 OC
对象,性能略差String
支持直接遍历Swift
提供了 String
和 NSString
之间的无缝转换// 字符串遍历
var str = "Hello, Swift"
for c in str.characters {
print(c)
}
let str1 = "Hello"
let str2 = "World"
let str3 = str1 + str2
let name = "why"
let age = 18
let info = "my name is \(name), age is \(age)"
let min = 3
let second = 4
let time = String(format: "%02d:%02d", arguments: [min, second])
import UIKit // 1.定义字符串:swift定义字符串不用加@,直接是c语言字符串“” let str = "hello world" // 2.遍历字符串中字符:空格也算是一个字符 for c in str.characters { print(c) } // 3.字符串的拼接 // 3.1.字符串之间的拼接 let str1 = "小码哥" let str2 = "IT教育" let result = str1 + str2 // 3.2.字符串和其他标识符之间的拼接 let age = 18 let name = "why" let height = 1.88 // 拼接其他标识符的格式: \(标识符的名称) let info = "my name is \(name), age is \(age), height is \(height)" // 3.3.字符串的格式化: %02d表示占位的意思,占两位,不足两位的用0去补位,arguments参数类型为一个数组,[min, second]表示一个数组,swift数组中可存放基本类型数据,用[]来表示 //音乐播放器: let min = 3 let second = 04 let timeStr = String(format: "%02d:%02d", arguments: [min, second]) // 4.字符串的截取:from 和 to都包括了 let urlString = "www.520it.com" // 将String类型转成NSString类型 string as NSString let header = (urlString as NSString).substring(to: 3) let middle = (urlString as NSString).substring(with: NSRange(location: 4, length: 5)) let footer = (urlString as NSString).substring(from: 10)
标签:
原文地址:http://www.cnblogs.com/cqb-learner/p/5883316.html