@hpp157
2016-08-09T16:04:40.000000Z
字数 1408
阅读 1804
swift
In swift ,strings are squences of Unicode characters,This means that they're able to store pretty much any characters that has ever been a part of human lanuage ,which is great news for making your app translate to other lanuuages
在swift中,字符串string是采用unicode编码的字符组成的序列。这意味着string能存储几乎所有(pretty much any)组成人类语言文字(比如英语,法语,中文)的字符。这对于那些正在准备把自己的app 转成别的语言的开发者来说是一个大好消息
Creating a string in swift is easy.You can create an empty sring by creating a string literal with nothing in it:
创建一个字符串是很容易的,你可以通过一个空的双引号的方式创建一个空的字符串:
let emptyString = "" //let 声明的是常量
还可以通过初始化字符串类型的方法创建一个空的字符串:
let anotherEmptySting = String()
用isEmpty属性来检测一个字符串是否为空:
emptyString.isEmpty //true
要拼接字符串的话,可以用“+” 号或者是“+=” :
var composingAString = "hello"
composingAString += ", world" //结果为hello,world
字符串实质上是一个字符对象的序列,每个字符代表一个Unicode字符,要循环string的每一个字符的话,可以使用for-in循环
Internally a string is a squence of Character objects, each represting a Unicode character.To loop over every characters in a string, you can use a for-in loop:
var reversedString =''
for character in "hello".characters{
reversedString =String(character) + reversedString
}
reversedString //"olleH"
要计算出一个string有多少个字符组成,可以使用count函数
To work out how many characters are in a string ,you use the count function:
"hello".characters.count //5
要改变一个字符的大小写,使用uppercaseString和lowercaseString属性,它们返回原来的string被修改后的版本
To change the case of a string,you use the uppercaseString and lowercaseString properties, which return modiffed versions of the original string:
string1.uppercaseString // 结果为HELLO
string2.lowecaseString //结果为 hello