swift -- 定义空字符串 hasPrefix hasSuffix trim split join range

前端之家收集整理的这篇文章主要介绍了swift -- 定义空字符串 hasPrefix hasSuffix trim split join range前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

// 定义空的字符串

var str1 = ""

var str2 = String()

str1.isEmpty // 判断字符串是否为空

// 输出字符串中所有的字符

var str3 = "As god name"

for c in str3{

  println(c)

}

Int.max // Int类型的最大值

Int.min // Int类型的最小值

var arr1 = ["c","oc","ios","swift"]

var count = 0

for name in arr1{

if name.hasPrefix("i"){ // hasPrefix 前缀

count++

count

}

}

count = 0

for name in arr1{

if name.hasSuffix("s"){ // hasSuffix 后缀

count++

count

}

}

var string1:String = " Hello,swift!! "

var string2 = "As god name"

// uppercaseString lowercaseString

string2.uppercaseString

string2.lowercaseString

// trim 去掉字符串前后的空白

string1.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

string1.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: " H"))

// split

string2.componentsSeparatedByString(" ")

var string3 = "welcome to play swift! Step-by-Step learn from now"

string3.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "-"))

// join

var string4 = "-"

string4.join(["one","two","three"])

var string5 = "--"

var arr = ["ios","swift","oc"]

string5.join(arr)

var str = "Welcome to play Swift! Step by Step learn Swift language from now"

// range

str.rangeOfString("Step")

str.rangeOfString("Step",options:NSStringCompareOptions.BackwardsSearch)

str.rangeOfString("welcome",options:NSStringCompareOptions.CaseInsensitiveSearch)

str.startIndex

str.endIndex

countElements(str)

var str = "Hello,playground"

let strRange = Range<String.Index>(start:str.startIndex,end:str.endIndex)

原文链接:https://www.f2er.com/swift/320880.html

猜你在找的Swift相关文章