1、Initializing an Empty String
var emtyString = “”
var anotherEmptyString = String()
两种初始化方法
2、Find out whether a String value is empty by checking its Boolean isEmpty property
if emptyString.isEmpy {
print(“Nothing to see here”)
}
3、override 计算机里面是覆盖的意思
6、you can create a stand-alone Character constant or variable from a single-character string literal by providing a Character type annotation.
你能通过一个简单的字符字符串文字,创建一个创建一个独立的字符常量或者变量。不过需要提供一个字符类型注释。
7、注意:字符类型和字符串类型是两种不同的类型。
Character value must contain a single character only
8、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)
9、Concatenating Strings and Characters构建字符串和字符
其中之一:you can append a Character value to a String type’s append() method:
welcome = “Hello there”
let exclamationMark: Character = “!”
welcome.append(exclamationMark)
print ——>result :”hello there”
10、Unicode
Unicode is an international standard for encoding,representing,and processing text in different writing systems.
Unicode是一种国际上的编码标准,用不同的书写系统代表和处理text。
又称呼万国码,是计算机科学领域里的一项业界标准。它对世界大部分的文字系统进行了整理、编码,使得电脑可以用更为简单的方式来呈现和处理文字。Uni—>universal通用的
11、Unicode Scalars Unicode的标量
A Unicode scalar is a unique 21-bit number for a character or modifier
A Unicode 标量是一个21比特的数字
A Unicode scalars is any Unicode code point in the range U+0000 to U+D7FF inclusive or U+E000 to U+10FFFF inclusive.Unicode scalars do not include the Unicode surrogate pair code points,which are the code points in the range U+D800 to U+DFFF inclusive.
A Unicode scalars 在范围U+0000 到U+D7FF 或者U+E000 到U+10FFFF.
Unicode scalars do not include the Unicode surrogate pair code points,which are the code points in the range U+D800 to U+DFFF inclusive
surrogate 代理 surrogate pair code points是什么???????
注意都是以U开头
12、The escaped special characters 转义的特殊字符
\0 (null character)
\\ (blackslash)
\t (horizontal tab)水平跳跃
\n (line Feed)换行
\r (carriage return) 回车
\” (double quote) 双引号
\ ‘ (single quote) 单引号
13、An arbitrary Unicode scalar,written as \u{n},where n is a 1-8 digit hexadecimal number with a value equal to a valid Unicode code point
要特别注意这里只针对数字,arbitrary算术的
后半句怎么理解:Unicode code point 是什么?????????
14、Extended Grapheme Clusters扩展的字形集群
an extended grapheme cluster is a sequence of one or more Unicode scalars that (when combined) produce a single human-readable character
一个拓展的字形集群是一个或者多个Unicode scalars (简单说就是多个unicode 的集合)
let aActe:Character = “\u{E9}\”
let combindEAcute:Character = “\u{65}\u{301}”
一个字符或者一种文字可以用多种方式表示,可以用一个单一的Unicode字符,也可以将其分开,用多个字符表示。
15、let unusualMenagerie = “Koala,snail,penguin,ddd”
print (“unusualMenagerie has \(unusualMenagerie.characcters.count) characters”)
可以用string.characters.count表示字符串的长度
16、Note that Swift’s use of extended grapheme clusters for Character values means that string concatenation and modification may not always affect a string’s character count
for example:
var word = “cafe”
print(“the number of characters in \(word) is \(word.characters.count)”)
result:print “the number of characters in cafe is 4”
print(“the number of character in \(word) is \(word.characters.count)”)
//result: “the number of characters in café is 4“
17、the count of the characters returned by the characters property is not always the same as the length property of an NNString that contains the same characters.The length of an NNString is based on the number of 16-bit code units within the string’s UTF-16 representation and not the number of Unicode extended grapheme clusters with the string.
characters的数量不总是跟NNString.length相同。两种获取值的方式不一样
18、String indices 指数
Each String value has an associated index type,String.Index,which corresponds to the position of each Character in the string.
19、In order to determine which Character is at a particular position,you must iterate over each Unicode scalar from the start or end of that String.For this reason .Swift strings cannot be indexed by integer values.
因为不知道具体字符Character的Characters(number of Unicode)
我们必须从从头或者从尾来计算,不能直接赋一个整数值。
@H_838_301@20、 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.??????怎么理解 @H_838_301@greeting[greeting.startIndex] >>>>G @H_838_301@greeting[greeting.index(before:greeting.endIndex)] >>>! @H_838_301@//be @H_838_301@greeting[greeting.index(after:greeting.startIndex]let index = greeting.index(greeting.startIndex,offsetBy:7)
@H_838_301@greeting[index]21、????greeting[greeting.endIndex]
????greeting.index(after:greeting.endIndex)
为什么错误
因为: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.
startIndex指向第一个元素,endInex指向string 最后一个元素的后面。所有不能用endIndex作为下标,但可以用before
22、You can use the startIndex and endIndex properties and the index(before:),index(after:),and index(_:offsetBy:}methods on any type that conforms to the Collection protocol,This includes String,as shown here,as well as collection types such as Array,Dictionary,and Set.
你可以用这些方法用在符合容易协议的类型。包括字符串、数组,字典和集合。
23、insert
insert(_:at:)
insert (contentOf:at:)
wecome.insert(“!”,at:welcome.endIndex)
24、let range = greeting.index(greeting.endIndex,offsetBy: -6)..<greeting.endIndex
注意..<
25、这些方法在string.array,dictionary,set都有用。
26、只要他们的形式和语言意义上相同(the same linguistic meaning and appearance),两个字符串就可以认为是相等。even if they are composed from different Unicode scalars behind the scenes.即使他们对用的Unicode 不一致。
比如:Voulez-vous un café? 中的é有两种拆分方式,这句话对应的Unicode有两种
let aAcuteQuestion = “Voules-vous un caf\u{E9}”
let combineEAcuteQuestion = “Voulez-zous un cafe\u{65}\u{301}”
尽管不一样我么也认为是规定意义上的相等(canonically)
但是:A 可以代表英语里面的首字母,也可以代表俄语里面的字母,尽管看上去完全一样,但是我们认为他们实不相等的。
let latinCatitalLetterA:Character = “\u{41}”
let cyrillicCapitalLetter:Character = “\u{0410}”
他们就不相等。
27、hasPrefix(_:)前缀比较
hasSuffix(_:)后缀比较
28、UTF-X encodes a sting as X-bit code units
for example:the UTF-32 encoding form encodes a sting as 32-bit code units
29、code Unit就是一个bit
30、!!对应的是U+203C,为什么需要三个字节呢。??????
我们知道它的值大小,8000多,这需要两个字节,可能还需要声明一个类型,加起来就三个了,其他的也一样。查找:DOUBLE EXCLAMATION MARK character可以明白。
31、文章的最后一部分讲述了字符串的保存以及占用的字节。
有utf8,urf16,unicodeScalars三种。
原文链接:https://www.f2er.com/swift/322028.html