//
// ViewController.swift
// Swift+String
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//var 变量 let常量 就不过多叙述了
self.view.backgroundColor = UIColor.yellow;
let my:NSString = "My name"
let name = " is 辉哥"
var myName = ""
let myfirst:NSString = "我的 名字"
myName = (my as String) + name
print(myName)
//计算字符串的长度
print(myfirst.length)//nsstring类型字符串
print(myName.characters.count)//string类型字符串
//获取字符串位置
var strA = "中国是最强大的国家"
let startindex = strA.startIndex//获取起始位置
print(startindex)
let endIndex = strA.endIndex//获取终止位置
print(endIndex)
let char = strA[strA.index(after: startindex)]//获取某个下标后一个下标对应的字符
print(char)
let char2 = strA[strA.index(before: endIndex)]//获取某个下标前一个下标对应的字符
print(char2)
let subString = strA[startindex...strA.index(startindex, offsetBy: 4)]//通过范围获取字符串中的某一个子串
print(subString)
let num = strA.range(of: "最强大")//获取某字符串在父串中的范围
print(num)
strA.append(" 世界上的")//追加字符串操作
print(strA)
strA.insert("~", at: strA.index(strA.startIndex, offsetBy: 10))//在指定位置出阿如一组字符
print(strA)
strA.replaceSubrange(strA.startIndex...strA.index(strA.startIndex, offsetBy: 9), with: "我们大中国永远屹立在")//在指定范围替换一个字符串
print(strA)
strA.remove(at: strA.index(before: strA.endIndex))//在指定位置删除一个字符
print(strA)
strA.removeSubrange(strA.startIndex...strA.index(strA.startIndex, offsetBy: 2))
print(strA)
var strB = "myname"
strB = strB.uppercased()//全部转为大写
print(strB)
strB = strB.lowercased()//全部转为小写
print(strB)
//字符串的连接
//string 型字符串
let str1:String = "上"
let str2:String = "海"
//使用" + "号连接两个字符串
var str3:String = str1 + str2
print(str3)
//在字符串的末尾拼接字符串,代替str3.extend("123456789")
str3.write("123456789")
print(str3)
//字符串比较
//sting型字符串
let str4:String = "SHANGHAI"
let str5:String = "shanghai"
//判断连个字符串是否相等
let bl1:Bool = str4 == str5
print(bl1)
//判断字符串是否为空
let bl2:Bool = str4.isEmpty
print(bl2)
//判断字符串前缀
let bl3:Bool = str4.hasPrefix("SHANG")
print(bl3)
//判断字符串后缀
let bl4:Bool = str4.hasSuffix("HAI")
print(bl4)
//比较两个字符串
let result1:ComparisonResult = str4.compare(str5)
print(result1)
//忽略大小写比较
let result2:ComparisonResult = str4.caseInsensitiveCompare(str5)
print(result2)
//NSSting 型字符串
let str6:NSString = "shanghai"
let str7:NSString = "SHANGHAI"
//判断两个字符串是否相等
let bl5:Bool = str6.isEqual(str7)
print(bl5)
//判断字符串前缀
let bl6:Bool = str6.hasPrefix("shang")
print(bl6)
//判断字符串后缀
let bl7:Bool = str6.hasSuffix("hai")
print(bl7)
//忽略大小写比较
let result3:ComparisonResult = str6.compare(str7 as String)
print(result3)
let result4:ComparisonResult = str6.caseInsensitiveCompare(str7 as String)
print(result4)
//字符串切割
//Sting 型字符串
let str11:String = "dong$fang$ming$zhu"
//字符串 按照字符切割
let arr1:Array = str11.components(separatedBy: "$")
print(arr1)
//自动按照" / "分割字符串 NSString特有,string没有
let str13:NSString = "Users/JHQ0228/Desktop"
let arr3:Array = str13.pathComponents
print(arr3)
//字符串与数字的相互转换 有一些用的比较少,就当做了解一下吧
// String 型字符串
// 字符 转 数字
let charr:Character = "A";
var charVal1:Int = 0
// 遍历字符转换为的字符串
for charStr in String(charr).unicodeScalars {
// 将取出的字符转换为 Int 型数字
charVal1 = Int(charStr.value)
}
// 字符串 转 数字
// 数字型字符串 转 Int 型数字,代替 intStr.toInt()
let intStr:String = "64"; let intVal1:Int? = Int(intStr)
// 数字 转 字符
let intVal2 = 65; let charVal2:Character = Character(UnicodeScalar(intVal2)!)
// 数字 转 字符串
// Int 型数字 转 字符串
let intVal3:Int = 123; let intStr1:String = intVal3.description
// Int32 型数字 转 字符串
let int32Val1:Int32 = 32; let int32Str1:String = int32Val1.description
// Int64 型数字 转 字符串
let int64Val1:Int64 = 64; let int64Str1:String = int64Val1.description
// Float 型数字转 字符串
let floatVal1:Float = 2.1; let floatStr1:String = floatVal1.description
// Double 型数字转 字符串
let doubleVal1:Double = 162.1; let doubleStr1:String = doubleVal1.description
// Bool 型 转 字符串
let boolVal1:Bool = true; let boolStr1:String = boolVal1.description
// NSString 型字符串
// 字符串 转 数字
// 字符串 转 Int 型数字
let intVal4:Int = NSString(string: "6432").integerValue
// 字符串 转 Int32 型数字
let int32Val2:Int32 = NSString(string: "32").intValue
// 字符串 转 Int64 型数字
let int64Val2:Int64 = NSString(string: "64").longLongValue
// 字符串 转 Float 型数字
let floatVal2:Float = NSString(string: "2.1").floatValue
// 字符串 转 Double 型数字
let doubleVal2:Double = NSString(string: "162.1").doubleValue
// 字符串 转 Bool 型
let boolVal2:Bool = NSString(string: "false").boolValue
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}