swift基础(一)字符串处理
2016-01-07 23:58
531 查看
1.swift介绍
1⃣️.swift是一个面向协议的编程,你说不定听说过面向对象编程,函数式编程,泛型编程。什么是面向协议,下面我来介绍下。
面向协议编程是在面向对象编程基础上演变而来。将程序设计过程中遇到的数据类型抽取由使用基类进行抽取改为使用协议,比如一个霸王龙,一个三角龙,我们就很容易想到一个基类,可能也会想到一个动物的通用描述。后者就是面向协议编程。那么学习swift的优势有哪些呢?
swift语言很容易学,混合类object—C JS和Python,语法简单。
swift语言功能强大,为编程人员提供了一边编程,一边预览自己的应用程序。并且可以提高性能。执行速度快。废话不多说了,下面就介绍关于字符串和swift语言的常量和变量。(来自对官方文档的学习)
Swift是一种类型安全的语言。类型安全就是说在编程的时候需要弄清楚变量的类型。如果您的代码部分需要一个字符串,你不能错误地传递一个整数类型。
因为Swift是类型安全的,它会在编译的时候就检查你的代码,任何类型不匹配时都会报错。这使得编程人员能够尽快捕获并尽可能早地在开发过程中修正错误。
类型检查可以在使用不同类型的值时帮助避免错误。但是,这并不意味着你必须指定每一个常量和变量所声明的类型。如果不指定你需要的类型,Swift使用类型推导来指定出相应的类型。类型推导使编译器在编译的时候通过你提供的初始化值自动推导出特定的表达式的类型。
类型推导使Swift比起C或Objective-C只需要更少的类型声明语句。常量和变量仍然显式类型,但大部分指定其类型的工作Swift已经为你完成了。
当你声明一个常量或变量并给出初始值类型的时候,类型推导就显得特别有用。这通常是通过给所声明的常量或变量赋常值来完成的。
//常量解释
// let apples = 3
// let oranges = 5 //
// let appleSummary = "I hava \(apples) apples."//反斜杠表示
// let fruitSummary = "I hava \(apples + oranges) pieces of fruit"
// print(appleSummary)
// print(fruitSummary)
//
//
//
// //变量详解
// var myVariable = 42
// myVariable = 50
// print(myVariable)
// var name = "guan"
// name = "nihao"
// print(name)
// var shoppingList = ["catfish","water","tulips"]//创建数组
// shoppingList[1] = "bottle of water"
// print(shoppingList)
// var occupations = ["Malcolm":"captain","kaylee":"mechanic"]
// occupations["qayne"] = "yrublic relations"
// print(occupations)
// let emptyArray = [String]()//创建一个空数组
// let emptyArray2 = []
// print(emptyArray2)
// let emptyDictionary = Dictionary<String,Float>()//创建一个空字典。
// let emptyDictionary2 = [:]//不知道字典类型时用这个表示空字典
// print(emptyArray)
// print(emptyDictionary)
// print(emptyDictionary2)
// let someSring = "Hello World"//通过字符串字面量进行初始化
一个二进制数表示
let binaryInteger = 0b10001 -》 17
一个八进制数表示
let octalInteger = 0o21 -》17
一个十六进制数表示
let hexadecimalInteger = 0x11 - 》17
科学计数法:
let decimalDouble = 12.1875
let exponentDouble = 1.218775e1 - 》12.1875
let hexadecimalDouble = 0xC.3p0 - 》12.1875
添加下划线便于阅读
let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_00_1
//转移特殊字符\0(空字符),\\反斜线,\t(水平制表符),\n换行符,\r回车符,\"双引号,\'单引号
//单字节Unicode标量,写成\xnn,nn为两位十六进制数
//双字节Unicode标量,写成\unnnn,其中nnnn为4位十六进制
//四字节Unicode标量,写成\Unnnnnnnn,nnnnnnnn为8位十六进制数
// let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"
// print(wiseWords)
// let dollarSign = "\u{24}"// Unicode scalar U + 0024
// let blackHeart = "\u{2665}"//Unicode scalar U + 2665
// let sparklingHeart = "\u{1f496}"//Unicode scalar u + 1f496
// print(dollarSign,blackHeart,sparklingHeart)
// //初始化空字符串
// var emptyString = ""
// var anotherEmptyString = String()
// if emptyString.isEmpty{
// print("Nothing to see here")
// }
//字符串的可变性
// var variableString = "horse"
// variableString += "and carriage"
// let constantString = "Highlander"
// constantString += "and another Highlander"//swift中是用常量和变量来声明NSString和 NSSMutableString
//值类型字符串
//意思是swift编译器会在字符串传递过程中默认复制,保证在函数或者方法中传递的是字符串的,明确指出它独自拥有,string类型每一个字符值都代表一个Unicode字符
// for character in "dog!".characters{
// print(character)
// }
// let yenSign : Character = "$"//通过字符字面量进行赋值,建立一个独立的字符常量或变量
// print(yenSign)
//计算字符数量
// let unusualMenagerie = "koala,snail,penguin,dromedary"
// print(unusualMenagerie.characters.count)
// 连接字符串和字符
// let string1 = "hello"
// let string2 = " there"
// let character1:Character = "!"
// let character2:Character = "?"
// //你不能连接俩个字符变量,或者字符串加字符,因为一个字符仅仅只能必须包含一个特定的字符
//
// var stringPlusString2 = string1 + string2
// print(stringPlusString2)
// stringPlusString2.append(character1)//改了这样string和character连接
// print(stringPlusString2)
// stringPlusString2.append(character2)
// print(stringPlusString2)
//
// var instruction = "look over"
// instruction += string2
// print(instruction)
//
// var welcome = "good morning"
// welcome.append(character1)
// print(welcome)
//字符串插值
// let multiplier = 3
// let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// print(message)
//将\(multiplier)插入到字符串字面量中。
var welcome = "hello"
welcome.insert("!", atIndex: welcome.startIndex.advancedBy(4))//在字符串某个位置插入字符
print(welcome)
welcome.insertContentsOf(" there".characters, at: welcome.endIndex.predecessor())//在字符串某个位置插入别的字符串
print(welcome)
welcome.removeAtIndex(welcome.endIndex.predecessor())//移除字符串某个位置的字符
print(welcome)
let range = welcome.endIndex.advancedBy(-1)..<welcome.endIndex
welcome.removeRange(range)//从某个特定位置移除字符
print(welcome)
//比较字符串
//1.字符串相等
// let quotation = "we're a lot alike,you and I."
// let sameQuotation = "we're a lot alike,you and I."
// if quotation == sameQuotation{
// print("These two strings are considered equal")
// }
// //2.后缀/前缀相等
// let 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;
// for scene in romeoAndJuliet{
// if scene.hasPrefix("Act 1 "){
// ++act1SceneCount
// }
// }
// print("There are \(act1SceneCount) scenes in Act 1")
//3.大写和小写字符串
// let normal = "Could you help me,please?"
// let shouty = normal.uppercaseString
// let whispered = normal.lowercaseString
// print(shouty,whispered)
//UTF-8代码单元集合(利用字符串的utf8属性进行访问)
//UTF-16代码单元集合(利用字符串的utf16属性进行访问)
//21位的Unicode标量值集合(利用字符串unicodeScalars属性进行访问)
// let dogString = "Dog!
1⃣️.swift是一个面向协议的编程,你说不定听说过面向对象编程,函数式编程,泛型编程。什么是面向协议,下面我来介绍下。
面向协议编程是在面向对象编程基础上演变而来。将程序设计过程中遇到的数据类型抽取由使用基类进行抽取改为使用协议,比如一个霸王龙,一个三角龙,我们就很容易想到一个基类,可能也会想到一个动物的通用描述。后者就是面向协议编程。那么学习swift的优势有哪些呢?
swift语言很容易学,混合类object—C JS和Python,语法简单。
swift语言功能强大,为编程人员提供了一边编程,一边预览自己的应用程序。并且可以提高性能。执行速度快。废话不多说了,下面就介绍关于字符串和swift语言的常量和变量。(来自对官方文档的学习)
Swift是一种类型安全的语言。类型安全就是说在编程的时候需要弄清楚变量的类型。如果您的代码部分需要一个字符串,你不能错误地传递一个整数类型。
因为Swift是类型安全的,它会在编译的时候就检查你的代码,任何类型不匹配时都会报错。这使得编程人员能够尽快捕获并尽可能早地在开发过程中修正错误。
类型检查可以在使用不同类型的值时帮助避免错误。但是,这并不意味着你必须指定每一个常量和变量所声明的类型。如果不指定你需要的类型,Swift使用类型推导来指定出相应的类型。类型推导使编译器在编译的时候通过你提供的初始化值自动推导出特定的表达式的类型。
类型推导使Swift比起C或Objective-C只需要更少的类型声明语句。常量和变量仍然显式类型,但大部分指定其类型的工作Swift已经为你完成了。
当你声明一个常量或变量并给出初始值类型的时候,类型推导就显得特别有用。这通常是通过给所声明的常量或变量赋常值来完成的。
//常量解释
// let apples = 3
// let oranges = 5 //
// let appleSummary = "I hava \(apples) apples."//反斜杠表示
// let fruitSummary = "I hava \(apples + oranges) pieces of fruit"
// print(appleSummary)
// print(fruitSummary)
//
//
//
// //变量详解
// var myVariable = 42
// myVariable = 50
// print(myVariable)
// var name = "guan"
// name = "nihao"
// print(name)
// var shoppingList = ["catfish","water","tulips"]//创建数组
// shoppingList[1] = "bottle of water"
// print(shoppingList)
// var occupations = ["Malcolm":"captain","kaylee":"mechanic"]
// occupations["qayne"] = "yrublic relations"
// print(occupations)
// let emptyArray = [String]()//创建一个空数组
// let emptyArray2 = []
// print(emptyArray2)
// let emptyDictionary = Dictionary<String,Float>()//创建一个空字典。
// let emptyDictionary2 = [:]//不知道字典类型时用这个表示空字典
// print(emptyArray)
// print(emptyDictionary)
// print(emptyDictionary2)
// let someSring = "Hello World"//通过字符串字面量进行初始化
一个二进制数表示
let binaryInteger = 0b10001 -》 17
一个八进制数表示
let octalInteger = 0o21 -》17
一个十六进制数表示
let hexadecimalInteger = 0x11 - 》17
科学计数法:
let decimalDouble = 12.1875
let exponentDouble = 1.218775e1 - 》12.1875
let hexadecimalDouble = 0xC.3p0 - 》12.1875
添加下划线便于阅读
let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_00_1
//转移特殊字符\0(空字符),\\反斜线,\t(水平制表符),\n换行符,\r回车符,\"双引号,\'单引号
//单字节Unicode标量,写成\xnn,nn为两位十六进制数
//双字节Unicode标量,写成\unnnn,其中nnnn为4位十六进制
//四字节Unicode标量,写成\Unnnnnnnn,nnnnnnnn为8位十六进制数
// let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"
// print(wiseWords)
// let dollarSign = "\u{24}"// Unicode scalar U + 0024
// let blackHeart = "\u{2665}"//Unicode scalar U + 2665
// let sparklingHeart = "\u{1f496}"//Unicode scalar u + 1f496
// print(dollarSign,blackHeart,sparklingHeart)
// //初始化空字符串
// var emptyString = ""
// var anotherEmptyString = String()
// if emptyString.isEmpty{
// print("Nothing to see here")
// }
//字符串的可变性
// var variableString = "horse"
// variableString += "and carriage"
// let constantString = "Highlander"
// constantString += "and another Highlander"//swift中是用常量和变量来声明NSString和 NSSMutableString
//值类型字符串
//意思是swift编译器会在字符串传递过程中默认复制,保证在函数或者方法中传递的是字符串的,明确指出它独自拥有,string类型每一个字符值都代表一个Unicode字符
// for character in "dog!".characters{
// print(character)
// }
// let yenSign : Character = "$"//通过字符字面量进行赋值,建立一个独立的字符常量或变量
// print(yenSign)
//计算字符数量
// let unusualMenagerie = "koala,snail,penguin,dromedary"
// print(unusualMenagerie.characters.count)
// 连接字符串和字符
// let string1 = "hello"
// let string2 = " there"
// let character1:Character = "!"
// let character2:Character = "?"
// //你不能连接俩个字符变量,或者字符串加字符,因为一个字符仅仅只能必须包含一个特定的字符
//
// var stringPlusString2 = string1 + string2
// print(stringPlusString2)
// stringPlusString2.append(character1)//改了这样string和character连接
// print(stringPlusString2)
// stringPlusString2.append(character2)
// print(stringPlusString2)
//
// var instruction = "look over"
// instruction += string2
// print(instruction)
//
// var welcome = "good morning"
// welcome.append(character1)
// print(welcome)
//字符串插值
// let multiplier = 3
// let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// print(message)
//将\(multiplier)插入到字符串字面量中。
var welcome = "hello"
welcome.insert("!", atIndex: welcome.startIndex.advancedBy(4))//在字符串某个位置插入字符
print(welcome)
welcome.insertContentsOf(" there".characters, at: welcome.endIndex.predecessor())//在字符串某个位置插入别的字符串
print(welcome)
welcome.removeAtIndex(welcome.endIndex.predecessor())//移除字符串某个位置的字符
print(welcome)
let range = welcome.endIndex.advancedBy(-1)..<welcome.endIndex
welcome.removeRange(range)//从某个特定位置移除字符
print(welcome)
//比较字符串
//1.字符串相等
// let quotation = "we're a lot alike,you and I."
// let sameQuotation = "we're a lot alike,you and I."
// if quotation == sameQuotation{
// print("These two strings are considered equal")
// }
// //2.后缀/前缀相等
// let 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;
// for scene in romeoAndJuliet{
// if scene.hasPrefix("Act 1 "){
// ++act1SceneCount
// }
// }
// print("There are \(act1SceneCount) scenes in Act 1")
//3.大写和小写字符串
// let normal = "Could you help me,please?"
// let shouty = normal.uppercaseString
// let whispered = normal.lowercaseString
// print(shouty,whispered)
//UTF-8代码单元集合(利用字符串的utf8属性进行访问)
//UTF-16代码单元集合(利用字符串的utf16属性进行访问)
//21位的Unicode标量值集合(利用字符串unicodeScalars属性进行访问)
// let dogString = "Dog!