swift学习之路(六)字符串和字符的常用操作
2016-05-30 20:18
381 查看
字符和字符串的常用操作包括连接,复制,比较,删除,插入。
(一)连接
连接比较简单,只需要用+就能将他们连接起来,不过值得注意的是单个字符常量或变量只能表示一个字符以及常量和变量的使用。
example:
import Foundation
let leftName = "hello "
let rightName = "world"
var thisName =
leftName + rightName
print(thisName)
(二)比较
1、比较两个字符串是否相等可以用“==”操作符。
example:
import Foundation
let str1 =
"hello world"
let str2 =
"hello world"
if str1 ==
str2 {
print("str1 is full equal to str2")
}
2、通过调用string类型的hasPrifix/hasSuffix方法检查字符串是否拥有特定的前缀/后缀。
example:
import Foundation
var exceptionLogs = [
"warning: Login In System with no passworld Check By DB",
"warning: View customer list without DB",
"Error: You Have No Jusrisdiction",
"warning: A Operate is no effect",
"Error: Illicit close is no effect"]
var warningCount =
0
var ErrorCount = 0
for attile
in exceptionLogs{
if attile.hasPrefix("warning"){
warningCount++
}
if attile.hasPrefix("Error"){
ErrorCount++
}
}
print("warning has
\(warningCount) and error has
\(ErrorCount)")//输出3,2
var DBcount = 0;
for attile
in exceptionLogs{
if attile.hasSuffix("Check By DB"){
DBcount++
}
}
print("have
\(DBcount) checked by DB")//输出1
string类型拥有uppercaseString 和 lowercaseString两个属性,使用它们来访问一个字符串的大写\小写版本。 example:
import Foundation
let str1 = "ABCDE"
let str2 = "abcde"
var str3 =
str1.lowercaseString
if str3 ==
str2{
print("they are equal")
}
(一)连接
连接比较简单,只需要用+就能将他们连接起来,不过值得注意的是单个字符常量或变量只能表示一个字符以及常量和变量的使用。
example:
import Foundation
let leftName = "hello "
let rightName = "world"
var thisName =
leftName + rightName
print(thisName)
(二)比较
1、比较两个字符串是否相等可以用“==”操作符。
example:
import Foundation
let str1 =
"hello world"
let str2 =
"hello world"
if str1 ==
str2 {
print("str1 is full equal to str2")
}
2、通过调用string类型的hasPrifix/hasSuffix方法检查字符串是否拥有特定的前缀/后缀。
example:
import Foundation
var exceptionLogs = [
"warning: Login In System with no passworld Check By DB",
"warning: View customer list without DB",
"Error: You Have No Jusrisdiction",
"warning: A Operate is no effect",
"Error: Illicit close is no effect"]
var warningCount =
0
var ErrorCount = 0
for attile
in exceptionLogs{
if attile.hasPrefix("warning"){
warningCount++
}
if attile.hasPrefix("Error"){
ErrorCount++
}
}
print("warning has
\(warningCount) and error has
\(ErrorCount)")//输出3,2
var DBcount = 0;
for attile
in exceptionLogs{
if attile.hasSuffix("Check By DB"){
DBcount++
}
}
print("have
\(DBcount) checked by DB")//输出1
string类型拥有uppercaseString 和 lowercaseString两个属性,使用它们来访问一个字符串的大写\小写版本。 example:
import Foundation
let str1 = "ABCDE"
let str2 = "abcde"
var str3 =
str1.lowercaseString
if str3 ==
str2{
print("they are equal")
}
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift自定义iOS中的TabBarController并为其添加动画
- Swift编程中的泛型解析
- Swift中定义二维数组的方法及遍历方法示例
- 简单分析Swift语言的一些基本特征
- 使用 Swift 语言编写 Android 应用入门
- Swift与C语言指针结合使用实例
- Swift心得笔记之控制流
- 用Swift构建一个简单的iOS邮件应用的方法
- 苹果公司推出的新编程语言Swift简介和入门教程
- Swift实现iOS应用中短信验证码倒计时功能的实例分享
- 探讨Swift数组和字典
- Swift中的指针操作和使用详细介绍
- Swift中使用正则表达式的一些方法
- Swift心得笔记之运算符
- Swift中使用可选类型完美解决占位问题
- Swift学习笔记之构造器重载