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

Swift 语言基础(2)-字符和字符串,第1张

概述字符串字面量 let someString = "Some string literal value"  let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"  // "Imagination is more important than knowledge" - Einstein  let do 字符串字面量

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


//□ 总结

以上是内存溢出为你收集整理的Swift 语言基础(2)-字符和字符串全部内容,希望文章能够帮你解决Swift 语言基础(2)-字符和字符串所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1088507.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存