Swift-字符串:String 的操作

前端之家收集整理的这篇文章主要介绍了Swift-字符串:String 的操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.判断是否是空值

//判断是否是空值@H_502_5@
 let@H_502_5@ str@H_502_5@:String = ""@H_502_5@
 if@H_502_5@ str@H_502_5@.isEmpty{
  print("str is nil"@H_502_5@)
 }
打印结果:str@H_502_5@ is nil

let@H_502_5@ str1:String = "hahahaha"@H_502_5@
   if@H_502_5@ str1.isEmpty{
      print("str is nil"@H_502_5@)
   }else@H_502_5@{
       print("str is \( str1)"@H_502_5@)
   }
   打印结果:str@H_502_5@ is hahahaha

2.获取字符数量

//获取字符数量@H_502_5@
 let@H_502_5@ str:String = "http://www.baidu.com"@H_502_5@
 print("the num is \( str.characters.count)"@H_502_5@)
 打印结果:the num is@H_502_5@ 20@H_502_5@

3.遍历字符

 //遍历字符@H_502_5@
 let str:String = "http://www.baidu.com"
 for charvalue in str.characters{
 print("--->\(charvalue)")
 }
 打印结果:
 --->h @H_502_5@--->t @H_502_5@--->t @H_502_5@--->p @H_502_5@--->:@H_502_5@ @H_502_5@--->/ @H_502_5@@H_502_5@--->/ @H_502_5@@H_502_5@--->w @H_502_5@--->w @H_502_5@--->w @H_502_5@--->. @H_502_5@--->b @H_502_5@--->a @H_502_5@--->i @H_502_5@--->d @H_502_5@--->u @H_502_5@--->. @H_502_5@--->c @H_502_5@--->o @H_502_5@--->m @H_502_5@

4.字符串是否有特定前缀/后缀

//字符串是否有前后@H_502_5@
   let@H_502_5@ str@H_502_5@:String = "http://www.baidu.com"@H_502_5@
   if@H_502_5@ str@H_502_5@ .hasPrefix("http://"@H_502_5@){
       print("hasPrefix--->http://"@H_502_5@)
   }
   if@H_502_5@ str@H_502_5@ .hasSuffix(".com"@H_502_5@){
       print("hasSuffix--->.com"@H_502_5@)
   }

打印结果:
hasPrefix--->http://@H_502_5@
hasSuffix--->.com

5.大小写转换

//大小写转换@H_502_5@
 let@H_502_5@ str@H_502_5@ = "Hello World!"@H_502_5@
 //大写@H_502_5@
 let@H_502_5@ uppercase = str@H_502_5@.uppercaseString
 //小写@H_502_5@
 let@H_502_5@ lowercase = str@H_502_5@.lowercaseString
 //首字母大写@H_502_5@
 let@H_502_5@ capitalized = str@H_502_5@.capitalizedString
 print("uppercase--->\(uppercase)"@H_502_5@)
 print("lowercase--->\(lowercase)"@H_502_5@)
 print("capitalized--->\(capitalized)"@H_502_5@)

 打印结果:
 uppercase--->HELLO WORLD!
 lowercase--->hello world!
 capitalized--->Hello World!

6.字符串数组

// 字符串数组@H_502_5@
 var@H_502_5@ strArray = [String]()
 strArray.append@H_502_5@("hello"@H_502_5@)
 strArray.append@H_502_5@(" "@H_502_5@)
 strArray.append@H_502_5@("world!"@H_502_5@)
 print@H_502_5@("strArray--->\(strArray)"@H_502_5@)
 //拼接字符串@H_502_5@
 print@H_502_5@("strArray--->\(strArray.joinWithSeparator("@H_502_5@"))"@H_502_5@)
 //拆分字符串@H_502_5@
 let newArray = strArray.split(""@H_502_5@)
 print@H_502_5@("strArray--->\(newArray)"@H_502_5@)

 打印结果:
 strArray--->["hello"@H_502_5@," "@H_502_5@,"world!"@H_502_5@]
 strArray--->hello world!
 strArray--->[[ArraySlice(["hello"@H_502_5@,"world!"@H_502_5@])]]

7.字符数组删除

//字符数组删除@H_502_5@
 var@H_502_5@ strArray = ["hello"@H_502_5@,"world"@H_502_5@,"!"@H_502_5@]
 print@H_502_5@("strArray--->\(strArray)"@H_502_5@)
 //删除第一个@H_502_5@
 strArray.removeFirst()
 print@H_502_5@("strArray--->\(strArray)"@H_502_5@)
 //删除最后一个@H_502_5@
 strArray.removeLast()
 print@H_502_5@("strArray--->\(strArray)"@H_502_5@)
//删除指定下标的元素@H_502_5@
 strArray .removeAtIndex(0@H_502_5@)
 print@H_502_5@("strArray--->\(strArray)"@H_502_5@)
//删除所有元素@H_502_5@
 strArray .removeAll()
 print@H_502_5@("strArray--->\(strArray)"@H_502_5@)

打印结果:
strArray--->["hello"@H_502_5@,"!"@H_502_5@]
strArray--->[" "@H_502_5@,"world"@H_502_5@]
strArray--->["world"@H_502_5@]
strArray--->[]
原文链接:https://www.f2er.com/swift/324329.html

猜你在找的Swift相关文章