swift学习笔记2 字符串

来源:互联网 发布:淘宝店铺客服链接获取 编辑:程序博客网 时间:2024/06/03 20:57

注:英文部分来自官方文档

字符串

  • 字符串可变性

    You indicate whether a particular String can be modified (or mutated) by assigning it to a variable (in which case it can be modified), or to a constant (in which case it cannot be modified):


把一个字符串符给一个变量,则这个变量是可变的;把字符串赋给常量,则是不可变的。
两个例子:
var variableString = "Horse"variableString += " and carriage"// variableString 现在是 "Horse and carriage"let constantString = "Highlander"constantString += " and another Highlander"//这会报个错- a constant string cannot be modified ( 常量字符串不能更改)

This approach is different from string mutation in Objective-C and Cocoa, where you choose between two classes (NSString and NSMutableString) to indicate whether a string can be mutated.
这种方式跟oc和 cocoa中不一样,在oc 和cocoa中 使用nsstring 和nsmutablestring来区分字符串是否可变。

  • 字符串是值类型

    Swift’s String type is a value type. If you create a new String value, that String value is copied when it is passed to a function or method, or when it is assigned to a constant or variable. In each case, a new copy of the existing String value is created, and the new copy is passed or assigned, not the original version.


swift中的字符串是值类型。如果你创建了一个新的字符串值,那么当把它穿给一个方法或函数,或则赋值给一个常量或变量是,该字符串的值将被拷贝。当前字符串的拷贝将会被创建,然后传递的或赋值的是这个拷贝,而不是原始的字符串。
- 字符
String values can be constructed by passing an array of Character values as an argument to its initializer:可以通过把字符数组作为参数穿给初始方法来构建字符串
let catCharacters: [Character] = ["C", "a", "t", "!", "��"]let catString = String(catCharacters)print(catString)// 打印 "Cat!��"
  • 拼接字符串和字符

String values can be added together (or concatenated) with the addition operator (+) to create a new String value:
可以用加号 + 来拼接字符串的值从而构建一个新的字符串:

let string1 = "hello"let string2 = " there"var welcome = string1 + string2// welcome 现在的值是 "hello there"

也可以使用+= 号:

var instruction = "look over"instruction += string2// instruction 现在的值是 "look over there"

You can append a Character value to a String variable with the String type’s append() method:
可以使用字符串的append()方法来把一个字符拼接到字符串的后面:

let exclamationMark: Character = "!"welcome.append(exclamationMark)// welcome 现在的值是 "hello there!"
  • 字符串的获取与修改
    可以使用下标语法(索引语法)来获取和修改一个字符串。

Each String value has an associated index type, String.Index, which corresponds to the position of each Character in the string.

每个字符串都有一个相关的index类型,对应着字符串中的每个字符。

Swift strings cannot be indexed by integer values.

swift中的字符串不能用整型值来做索引

Use the startIndex property to access the position of the first Character of a String. The endIndex property is the position after the last character in a String. As a result, the endIndex property isn’t a valid argument to a string’s subscript. If a String is empty, startIndex and endIndex are equal.

使用startIndex来获取字符串的第一个字符。endIndex表示字符串中在最后一个字符之后的位置。所以对于一个字符串来说,endIndex并不是一个有效的下标。如果一个字符串为空,startIndex和endIndex是相等的。

You access the indices before and after a given index using the index(before:) and index(after:) methods of String. To access an index farther away from the given index, you can use the index(_:offsetBy:) method instead of calling one of these methods multiple times.

可以通过index(before:) 和index(after:)方法来获取一个给定的索引的前一个 和后一个索引。用index(_:offsetBy:)来获取给定索引的后几位索引,而不需要多次调用上面的那些方法。

You can use subscript syntax to access the Character at a particular String index
可以使用下标语法(索引语法)来获取一个字符串中的特定的字符。

let greeting = "Guten Tag!"greeting[greeting.startIndex]// Ggreeting[greeting.index(before: greeting.endIndex)]// !greeting[greeting.index(after: greeting.startIndex)]// ulet index = greeting.index(greeting.startIndex, offsetBy: 7)greeting[index]// agreeting[greeting.endIndex] // 错误的写法greeting.index(after: greeting.endIndex) // 错误的写法
0 0
原创粉丝点击