文章目录
  1. 1. Swift学习-03String和Character
    1. 1.1. String和Character
    2. 1.2. String基础操作
    3. 1.3. 使用更多字符串功能
    4. 1.4. String.Index和Range

Swift学习-03String和Character

String和Character

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//----------------3.1 String和Character---------------//
/**
* let String ====> NSString
* var String ====> NSMutableString
*/
var str = "你好, QingU\u{1F496}"
let str2 = "I'm a mutable string"
//Initializing an Empty String
var emptyString = ""
var anotherEmptyString = String()
emptyString += "hi"
emptyString.isEmpty
anotherEmptyString.isEmpty
//字符串遍历
for c in str
{
println(c)
}
var ch:Character = "!"
str.append(ch)
str += str2
//字符串长度,中文算一个字符,uicode符号算1个字符
countElements(str)
//OC字符串,uicode符号算2个字符
var str3:NSString = str
str3.length

String基础操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//----------------3.2 String基础操作---------------//
var str4 = "你好, QingU\u{1F496}"
//拼接不赋值
str4 + "么么哒"
str4
//拼接赋值
str4 += "么么哒"
str4
let ch2:Character = "!"
str4.append(ch)
str4
//String InterPolation插值变量
let imInt:Int = 2
let imDouble:Double = 3.1415926
let imBool:Bool = true
let imString:String = "Hello"
//元组
let imTuple:(Int, Int) = (2,4)
//可选型
let imOptional:Int? = nil
let imCharacter:Character = "!"
println("Int:\(imInt)\nDouble:\(imDouble)")
//String Comparing 字符串比较-->字典序
let str_a = "abc"
let str_b = "abc"
str_a == str_b
let str_c = "abd"
str_a < str_c
let str_d = "abcd"
str_d < str_c
//Prefix and Suffix--->字符串前缀和后缀
//.hasPrefix and .HasSuffix
let names = [
"第一章 1.哈哈哈哈-",
"第二章 2.嘿嘿嘿",
"第二章 3.老地方-",
"第二章 4.我们",
"第二章 5.jackChang-",
"第二章 6.UQingU-",
"第三章 7.哇哇哇",
"第三章 8.噢噢噢噢-",
"第三章 9.偶偶-",
"第三章 10.咳咳咳-"
]
var count = 0
//遍历数组计算前缀是”第二章“的对象有几个
for name in names
{
if name.hasPrefix("第二章")
{
count++
}
}
count
count = 0
//遍历数组计算后缀是”-“的对象有几个
for name in names
{
if name.hasSuffix("-")
{
count++
}
}
count

使用更多字符串功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//---------3.3 使用更多字符串功能-----------//
//import Foundation
var str5 = "Hello, UQing!"
str5.capitalizedString
str5
str5.uppercaseString
str5
str5.lowercaseString
str5
//trim,对字符串前后无用部分裁剪
var str6 = " !hi!!! "
//裁剪空白
str6.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
//裁剪任意字符,裁剪" !"
str6.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: " !"))
//split 按固定格式截取
var str7 = "welcome to play swift"
str7.componentsSeparatedByString(" ")
str7 = "welcome to play swift!Step-by-step from now"
str7.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: " !-"))
//join 连字符
var str8 = "-"
str8.join(["1", "2", "3"])

String.Index和Range

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//---------3.4 String.Index和Range-----------//
var str9 = "Welcome to play swift! step by step learn Swift language from now!"
//range
str9.rangeOfString("step")
//从后往前数
str9.rangeOfString("step", options: NSStringCompareOptions.BackwardsSearch)
//不区分大小写
str9.rangeOfString("welcome", options:NSStringCompareOptions.CaseInsensitiveSearch)
str9.startIndex
str9.endIndex
let strRange = Range<String.Index>(start: str9.startIndex, end: str9.endIndex)
//从0-10范围内查找字符
let startIndex = str9.startIndex
let endIndex = advance(str9.startIndex, 10)
let searchRange = Range<String.Index>(start: startIndex, end: endIndex)
str9.rangeOfString("Step", options: NSStringCompareOptions.CaseInsensitiveSearch, range: searchRange)
//substring
var toIndex = advance(str9.startIndex, 4)
str9.substringToIndex(toIndex)
str9.substringFromIndex(toIndex)
str9.substringWithRange(searchRange)
//insert
var insertIndex = advance(str9.startIndex, 22)
str9.insert("!", atIndex: insertIndex)
//remove
str9.removeAtIndex(insertIndex)
str9.removeRange(searchRange)
str9
//replace
var replaceStr = advance(str9.startIndex, 13)
str9.stringByReplacingCharactersInRange(Range<String.Index>(start:str9.startIndex, end:replaceStr), withString: "HELLO")
文章目录
  1. 1. Swift学习-03String和Character
    1. 1.1. String和Character
    2. 1.2. String基础操作
    3. 1.3. 使用更多字符串功能
    4. 1.4. String.Index和Range