//: Playground - noun: a place where people can play
import UIKit
//--------------------------運(yùn)算符
/*1、算術(shù)運(yùn)算符 2、比較運(yùn)算符 3伐弹、邏輯運(yùn)算符 4、位運(yùn)算符 5榨为、賦值運(yùn)算符 6 區(qū)間運(yùn)算符 7惨好、其他運(yùn)算符*/
//算術(shù)運(yùn)算符+ - * / % += -=
//注意:不同數(shù)據(jù)類(lèi)型不能進(jìn)行運(yùn)算
varA =20
varB =30
print("A + B 結(jié)果為\(A+B)")
print("A - B 結(jié)果為\(A-B)")
print("A * B 結(jié)果為\(A*B)")
print("A / B 結(jié)果為\(A/B)")
A+=B
print(A)
A=80
B=30
print("A % B 結(jié)果為\(A%B)")
//比較運(yùn)算符 == != > < >= <=
print("A == B 結(jié)果為\(A == B)")
print("A != B 結(jié)果為\(A != B)")
print("A >= B 結(jié)果為\(A >= B)")
//邏輯運(yùn)算符 &&? || 随闺! 1日川、&如果運(yùn)算符兩側(cè)都為true則為true? 2、|| 至少或只有一側(cè)返回true則為true? 3矩乐、龄句!邏輯非 布爾值反,使得true變false散罕,false變true
var M = true
var N = false
print("M && N 結(jié)果為\(M && N)")//兩側(cè)是一樣的即為true分歇,不一樣的即為false
print("M || N 結(jié)果為\(M || N)")
print("!M 結(jié)果為\(!M)")
//?????????????練習(xí)page39
//--------------------------一、字符
/*
Character字符數(shù)據(jù)類(lèi)型
?swift是用雙引號(hào)欧漱,OC用單引號(hào)
?字符里定義一個(gè)字符
*/
let a:Character = "b"
var b:Character
b="a"
//--------------------------二职抡、字符串
//1??、定義字符串
//創(chuàng)建一個(gè)空字符串
letemptyStr =""
//定義一個(gè)不可變字符串
let string = "hello swift"
//NSString *string = "hello swift"
//定義一個(gè)可變字符串
var ableStr = "you can change it"
//2??误甚、字符串的使用
//獲取字符串的長(zhǎng)度
letlengCount =string.count
letlengthCount =string.lengthOfBytes(using:String.Encoding.utf8)
//遍歷字符串的所有字符
for c in string {
? ? print(c)
}
//index是下標(biāo)缚甩,char是字符
for(index,char)instring.enumerated() {
? ? print("下標(biāo)是\(index)對(duì)應(yīng)的值是\(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 ="您有\(zhòng)(nums)張優(yōu)惠券"
print("您有\(zhòng)(nums)張優(yōu)惠券")
str1 += str2//也可以通過(guò)賦值運(yùn)算符(+=)將一個(gè)字符串或者字符添加到一個(gè)已經(jīng)存在的字符串變量上
//var num = 4
//str2 += num //不同數(shù)據(jù)類(lèi)型不能相加
//str2 + num
//3.2字符串和其他標(biāo)志符之間的拼接
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字符串拼接過(guò)程中進(jìn)行格式化
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
//通過(guò)下標(biāo)方法截取 to:后面接的是字符的長(zhǎng)度
//注意:空格占一個(gè)字符串
print("截取到4的字符串是\(strname.substring(to: 4))")
varfour ="\(strname.substring(to:4))"
print("從4開(kāi)始截取的字符串是\(strname.substring(from: 4))")
letranges =strname.substring(with:NSMakeRange(1,5))
print("1到5范圍內(nèi)的字符串\(ranges)")
//9??轉(zhuǎn)換大寫(xiě)谱净、小寫(xiě)
nameStr.uppercased()
nameStr.lowercased()
//?????????????練習(xí)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
//相當(dāng)于 myscore = myscore + 100
//清屏網(wǎng)、航歌擅威、菜鳥(niǎo)教程壕探、幕課網(wǎng)、網(wǎng)易云課堂