加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 语言基础(2)-字符和字符串

发布时间:2020-12-14 02:17:45 所属栏目:百科 来源:网络整理
导读:字符串字面量 let someString = "Some string literal value" wiseWords = ""Imagination is more important than knowledge" - Einstein" // "Imagination is more important than knowledge" - Einstein dollarSign "x24" // $,Unicode scalar U+0024 b

字符串字面量

let someString = "Some string literal value"

wiseWords = ""Imagination is more important than knowledge" - Einstein"
// "Imagination is more important than knowledge" - Einstein
dollarSign "x24" // $,Unicode scalar U+0024
blackHeart "u2665" // ?,Unicode scalar U+2665
sparklingHeart "U0001F496" //,□,Unicode scalar U+1F496

初始化空字符串

var emptyString "" // empty string literal
anotherEmptyString = String() // initializer syntax
// these two strings are both empty,and are equivalent to each other


if emptyString.isEmpty {
println("Nothing to see here")
}
// prints "Nothing to see here"

字符串可变性
variableString "Horse"
+= " and carriage"
// variableString is now "Horse and carriage"

constantString "Highlander"
constantString " and another Highlander"
// this reports a compile-time error - a constant string cannot be modified


字符串是值类型

Swift’s String type is a value type. If you create a new String value,that 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 value is
created,and the new copy is passed or assigned,not the original version.


字符处理

for character in "Dog!□" {
(character// D
// o
// g
// !
// □

yenSign: Character "¥"

字符计数

unusualMenagerie "Koala,Snail,Penguin,Dromedary "
"unusualMenagerie has (countElementsunusualMenagerie)) characters"// prints "unusualMenagerie has 40 characters"

连接字符串和字符

string1 "hello"
string2 " there"
character1"!"
character2"?"
stringPlusCharacter + character1 // equals "hello!"
stringPlusString // equals "hello there"

characterPlusString // equals "!hello"
characterPlusCharacter character2 // equals "!?"

instruction "look over"
+= string2
// instruction now equals "look over there"
welcome "good morning"
character1
// welcome now equals "good morning!"


字符串插补


let multiplier = 3
let message = " ( multiplier ) times 2.5 is ( Double ( multiplier ) * 2.5 ) "
// message is "3 times 2.5 is 7.5"

字符串比较

quotation "We're a lot alike,you and I."
sameQuotation == println"These two strings are considered equal"// prints "These two strings are considered equal"

romeoAndJuliet = [
"Act 1 Scene 1: Verona,A public place",
"Act 1 Scene 2: Capulet's mansion""Act 1 Scene 3: A room in Capulet's mansion""Act 1 Scene 4: A street outside Capulet's mansion""Act 1 Scene 5: The Great Hall in Capulet's mansion""Act 2 Scene 1: Outside Capulet's mansion""Act 2 Scene 2: Capulet's orchard""Act 2 Scene 3: Outside Friar Lawrence's cell""Act 2 Scene 4: A street in Verona""Act 2 Scene 5: Capulet's mansion""Act 2 Scene 6: Friar Lawrence's cell"]

var act1SceneCount = 0
fo r scene in scenehasPrefix"Act 1 ") {
++act1SceneCount
}


println"There are act1SceneCount) scenes in Act 1"// prints "There are 5 scenes in Act 1"

mansionCount = 0
cellCount hasSuffix"Capulet's mansion") {

mansionCount
} else if scene"Friar Lawrence's cell"cellCount
}
"mansionCountmansion scenes; (cellCountcell scenes")
// prints "6 mansion scenes; 2 cell scenes"


字符串大小写转换

normal "Could you help me,please?"
shouty normaluppercaseString
// shouty is equal to "COULD YOU HELP ME,PLEASE?"
whispered lowercaseString
// whispered is equal to "could you help me,please?"


utf

codeUnit dogStringutf8 printcodeUnit"n"// 68 111 103 33 240 159 144 182

utf16 print// 68 111 103 33 55357 56374

scalar unicodeScalars scalarvalue// 68 111 103 33 128054


//□

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读