swift 笔记2

2016-04-27  本文已影响0人  魂淡峻

字符串的介绍

字符的定义

字符串的使用

获取字符串的长度

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])

字符串的截取

let myStr = "www.520it.com"
var subStr = (myStr as NSString).substringFromIndex(4)
subStr = (myStr as NSString).substringToIndex(3)
subStr = (myStr as NSString).substringWithRange(NSRange(location: 4, length: 5))
// 1.定义字符串
let str = "www.520it.com"

// 2.截取开始位置
let fromIndex = str.startIndex.advancedBy(3)
let header = str.substringFromIndex(fromIndex)

// 3.截取结束位置
let toIndex = str.endIndex.advancedBy(-3)
let footer = str.substringToIndex(toIndex)

// 4.截取中间的字符串
let range = Range(start: str.startIndex.advancedBy(4), end: str.endIndex.advancedBy(-4))
let middle = str.substringWithRange(range)
上一篇 下一篇

猜你喜欢

热点阅读