swift學習筆記2 字符串

注:英文部分來自官方文檔

字符串

  • 字符串可變性

    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]
// G
greeting[greeting.index(before: greeting.endIndex)]
// !
greeting[greeting.index(after: greeting.startIndex)]
// u
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index]
// a

greeting[greeting.endIndex] // 錯誤的寫法
greeting.index(after: greeting.endIndex) // 錯誤的寫法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章