Swift 3.0 字符串

来源:互联网 发布:网络打黄油是什么意思 编辑:程序博客网 时间:2024/06/14 16:42

1、字符串面量: 在代码中包含一段预定义的字符串值作为字符串字面量。字符串字面量是由双引号 (“”) 包裹着的具有固定顺序的文本字符集。 字符串字面量可以用于为常量和变量提供初始值。
例如:

let string = "Hello Swift 3.0";print(string);

2、初始化字符串

var emptyString = "";          // 空字符串字面量var emptyString1 = String();  // 初始化方法

3、判断字符串是否为空

if emptyString.isEmpty{    print("空字符串");}

4、字符串的可变性
在 Objective-C 和 Cocoa 中,您需要通过选择两个不同的类(NSString和NSMutableString)来指定字符串是否可以被修改,而在Swift 3.0 用 var 和 let 来判断字符串是否可以被修改。

var variableString = "Hello"variableString += "Swift 3.0";print(variableString);

输出结果:

HelloSwift 3.0

5、字符串是值类型
创建了一个新的字符串,那么当其进行常量、变量赋值操作,或在函数/方法中传递时,会进行值拷贝。在实际编译时,Swift 编译器会优化字符串的使用,使实际的复制只发生在绝对必要的情况下,这意味着您将字符串作为值类型的同时可以获得极高的性能。

6、使用字符
1) 可通过for-in循环来遍历字符串中的characters属性来获取每一个字符的值

let string = "Swift";var arrays = string.characters;for charactor in arrays{    print(charactor);}/*Hello*/

2) 通过标明一个Character类型并用字符字面量进行赋值,可以建立一个独立的字符常量或变量

let charactorChar :Character = "!";print("charactorChar is \(charactorChar)");

3) 字符串可以通过传递一个值类型为Character的数组作为自变量来初始化

let charactors:[Character] = ["H","e","l","l","o"];let helloString = String(charactors);print("helloString is \(helloString)");

7、连接字符串

1) 字符串可以通过加法运算符(+)相加在一起(或称“连接”)创建一个新的字符串

let string1 = "hello";let string2 = "Swift";let string3 =  string1 + string2;print("string3 is  \(string3)");// 输出  string3 is  helloSwift

2) 也可以通过加法赋值运算符 (+=) 将一个字符串添加到一个已经存在字符串变量上

var variablestring = "I am ";variablestring += string2;print("variablestring is \(variablestring)");// 输出 variablestring is I am Swift

3) 可以用append()方法将一个字符附加到一个字符串变量的尾部

variablestring.append("!");print("variablestring is \(variablestring)");// 输出 variablestring is I am Swift!

注意: 不能将一个字符串或者字符添加到一个已经存在的字符变量上,因为字符变量只能包含一个字符。

特殊字符: 字符串字面量可以包含以下特殊字符:转义字符\0(空字符)、\(反斜线)、\t(水平制表符)、\n(换行符)、\r(回车符)、\”(双引号)、\’(单引号)。

8、计算字符数量
如果想要获得一个字符串中Character值的数量,可以使用字符串的characters属性的count属性

let  calculstring = "jack";print("calculstring has \(calculstring.characters.count) charactor");//输出结果 calculstring has 4 charactor

9、访问字符串

let visitString = "Hello Swift";//获取第一个字符print("visitString 的第一个字符 :\(visitString[visitString.startIndex])");// 这里输出 visitString 的第一个字符 :H//获取最后一个字符print("visitString 的最后一个字符 :\(visitString[visitString.index(before: visitString.endIndex)])");// 这里输出  visitString 的最后一个字符 :t//获取下第二个字符print("visitString 的第二个字符 : \(visitString[visitString.index(after: visitString.startIndex)])")// 这里输出 visitString 的第二个字符 : e// 通过偏移量获取索引 第一个字符 往后偏移 4个单位(不包括起始位置) (备注: 如果你把字符串当数组来理解,你会发现 0 就在数组的自四个位置)let index = visitString.index(visitString.startIndex, offsetBy: 4);print("\(visitString[index])");// 这里输出  o// 使用 characters 属性的 indices 属性会创建一个包含全部索引的范围(Range),用来在一个字符串中访问单个字符for index in visitString.characters.indices{    print("通过下标得到每一个字符:\(visitString[index])");}// 这里输出/*通过下标得到每一个字符:H通过下标得到每一个字符:e通过下标得到每一个字符:l通过下标得到每一个字符:l通过下标得到每一个字符:o通过下标得到每一个字符: 通过下标得到每一个字符:S通过下标得到每一个字符:w通过下标得到每一个字符:i通过下标得到每一个字符:f通过下标得到每一个字符:t*/

10、 大小写转化

let changeString = "hello";print("\(changeString.uppercased())")  //转化成大写print("\(changeString.lowercased())")  // 转化成小写

11、插入
调用 insert(_:at:) 方法可以在一个字符串的指定索引插入一个字符,调用 insert(contentsOf:at:) 方法可以在一个字符串的指定索引插入一个段字符串。

//插入单个字符var welocome = "hello";welocome.insert("!", at: welocome.endIndex);print("welocome = \(welocome)");//welocome = hello!//插入一段字符串到某个位置   contentsOf : "字符串".characters 固定语法welocome.insert(contentsOf: "there".characters, at: welocome.index(before: welocome.endIndex));print("welocome = \(welocome)");//welocome = hello there!

12、删除
调用 remove(at:) 方法可以在一个字符串的指定索引删除一个字符

welocome.remove(at: welocome.index(before: welocome.endIndex));print("welocome = \(welocome)");//welocome = hello there// 调用 removeSubrange(_:) 方法可以在一个字符串的指定索引删除一个子字符串。let range = welocome.index(welocome.startIndex,offsetBy:6)..<welocome.endIndex;welocome.removeSubrange(range);print("welocome = \(welocome)");// welocome = hello//指定位置删除welocome.remove(at: welocome.index(before: welocome.endIndex));print(welocome);//hello

12、比较字符串

字符串/字符可以用等于操作符(==)和不等于操作符(!=)

let quotation = "We're a lot alike, you and I."let sameQuotation = "We're a lot alike, you and I."if quotation == sameQuotation{    print("相等");}// 前缀相等let preString01 = "date";if preString01.hasPrefix("da"){    print("preString01 前缀有da");}// 后缀相等if preString01.hasSuffix("te"){    print("preString01 后缀有te");}

13、字符串替换

var replace:String = "Swigt";if replace.contains("g"){    print("包含了g");    //将g替换成f    var s = replace.replacingOccurrences(of: "g", with: "f");    print(s);}// 指定范围范围let replaceRangeAll = replace.startIndex...replace.index(before:replace.endIndex);replace.replaceSubrange(replaceRangeAll, with: "Java");print(replace);输出结果:包含了gSwiftJava

14、字符串截取

// 截取到第二个字符串,但是不包括第二个字符串let subToString = replace.substring(to: visitString.index(after: visitString.startIndex));print(subToString);//从第二个字符串开始截取,到结束let subFromString = replace.substring(from: visitString.index(after: visitString.startIndex));print(subFromString);//指定范围截取 (可以是闭区间,也可以是开区间)let subRange = replace.index(after: replace.startIndex)..<replace.endIndex;let subRangeString = replace.substring(with: subRange)print(subRangeString);

输出结果:

Javaava

总结:
个人理解 : 字符串可理解成 以字符组合成的数组 。
掌握如何访问字符串,对字符串的增删改查就很好理解了。
例如 : 统一格式 字符串[ 访问字符的下标 ]
下标也很容易总结:
除了startIndex 外,其它位置的都是 “字符串.index ”开头

0 0
原创粉丝点击