//: Playground - noun: a place where people can play
import UIKit
//--------------------------运算符
/*1、算术运算符 2、比较运算符 3、逻辑运算符 4、位运算符 5、赋值运算符 6 区间运算符 7、其他运算符*/
//算术运算符+ - * / % += -=
//注意:不同数据类型不能进行运算
varA =20
varB =30
print("A + B 结果为\(A+B)")
print("A - B 结果为\(A-B)")
print("A * B 结果为\(A*B)")
print("A / B 结果为\(A/B)")
A+=B
print(A)
A=80
B=30
print("A % B 结果为\(A%B)")
//比较运算符 == != > < >= <=
print("A == B 结果为\(A == B)")
print("A != B 结果为\(A != B)")
print("A >= B 结果为\(A >= B)")
//逻辑运算符 && || ! 1、&如果运算符两侧都为true则为true 2、|| 至少或只有一侧返回true则为true 3、!逻辑非 布尔值反,使得true变false,false变true
var M = true
var N = false
print("M && N 结果为\(M && N)")//两侧是一样的即为true,不一样的即为false
print("M || N 结果为\(M || N)")
print("!M 结果为\(!M)")
//练习page39
//--------------------------一、字符
/*
Character字符数据类型
swift是用双引号,OC用单引号
字符里定义一个字符
*/
let a:Character = "b"
var b:Character
b="a"
//--------------------------二、字符串
//1⃣️、定义字符串
//创建一个空字符串
letemptyStr =""
//定义一个不可变字符串
let string = "hello swift"
//NSString *string = "hello swift"
//定义一个可变字符串
var ableStr = "you can change it"
//2⃣️、字符串的使用
//获取字符串的长度
letlengCount =string.count
letlengthCount =string.lengthOfBytes(using:String.Encoding.utf8)
//遍历字符串的所有字符
for c in string {
print(c)
}
//index是下标,char是字符
for(index,char)instring.enumerated() {
print("下标是\(index)对应的值是\(char)")
}
//3⃣️字符串的拼接\()\()
varstr1 =" hello"
varstr2 =" world"
varstr3 =str1+str2
//NSString *str3 = [NSString StringWithformat:"%@%@",str1,str2]
print(str3)
str3.append(str1)
print(str3)
varnums =10
if nums>9 {
nums-=1
}
varstrInfo ="您有\(nums)张优惠券"
print("您有\(nums)张优惠券")
str1 += str2//也可以通过赋值运算符(+=)将一个字符串或者字符添加到一个已经存在的字符串变量上
//var num = 4
//str2 += num //不同数据类型不能相加
//str2 + num
//3.2字符串和其他标志符之间的拼接
letmoney =2000
letstr ="元"
varnewStr ="\(money)\(str)"
leta1 ="Tina"
leta2 =20
leta3 ="178"
let info = "my name is\(a1),age is\(a2),height is\(a3)"
print(info)
let before:NSString = "http:www.baidu.com",after="/news"
lethttp =before.strings(byAppendingPaths: [after])
print(http)
//before + after
//3.3字符串拼接过程中进行格式化
letmin =3
letsecond =22
lettimer =String(format:"%03d:%02d.%2d", arguments:[min,second,second])
print(timer)
//before.appendingFormat(<#T##format: NSString##NSString#>, <#T##args: CVarArg...##CVarArg#>)
//4⃣️判断字符串是否为空
if emptyStr.isEmpty{
print("empty is nil")
}
if emptyStr.count == 0 {
print("empty is nil")
}
//5⃣️比较字符串
letstrValue1 ="我们一样"
letstrValue2 ="我们一样"
if strValue1 == strValue2 {
print("same")
}
//6⃣️判断前缀后缀
let first = "hello swiftsss"
if first.hasPrefix("hel"){
print("含有该前缀")
}
if first.hasSuffix("ft") {
print("含有该后缀")
}
//7⃣️判断是否包含
if first.contains("h") {
print("包含该字符")
}
//8⃣️截取字符串
//NSSting的截取方法
var nameStr = "my name is Tom"
let strname:NSString = nameStr as NSString
//通过下标方法截取 to:后面接的是字符的长度
//注意:空格占一个字符串
print("截取到4的字符串是\(strname.substring(to: 4))")
varfour ="\(strname.substring(to:4))"
print("从4开始截取的字符串是\(strname.substring(from: 4))")
letranges =strname.substring(with:NSMakeRange(1,5))
print("1到5范围内的字符串\(ranges)")
//9⃣️转换大写、小写
nameStr.uppercased()
nameStr.lowercased()
//练习page122
//var lowstr:String = "I want to search your location"
//lowstr.startIndex
//lowstr.endIndex
//lowstr.remove(at: lowstr.startIndex)
//lowstr.insert("H", at: lowstr.startIndex)
//print(lowstr)
//let strIndex1:String.Index = lowstr.index(of: "a")!
//let strIndex2:String.Index = lowstr.index(of: "y")!
//let subIndex:String.Index = lowstr.index(lowstr.startIndex, offsetBy: 5)
//lowstr.remove(at: subIndex)
//print(lowstr)
varmyscore =5
myscore += 100
//相当于 myscore = myscore + 100
//清屏网、航歌、菜鸟教程、幕课网、网易云课堂