Swift 3.0 -字符串

毛弘博
2023-12-01
//
//  main.swift
//  Swift-字符串
//
//  Created by yidong on 16/9/28.
//  Copyright © 2016年 东哥. All rights reserved.
//

import Foundation
//1.0
/*
 字符串/字符 的定义
 */

//字符串变量
var str1 = "hello"

//字符串常量
let str2 = "swift3.0"

//声明为nil,
var str3:String?

//空字符串
let str4 = String()

//空字符串 提倡用这样的字面量语法声明,类型可不指定,swift自动识别
let str5 = ""
//字符
var char1:Character = "m"
var p_str1 = ""

//2.0
/*
 字符串/字符 的拼接
 */
    
    p_str1 = str1+str2
    p_str1 = String(format:"%@~%@",str1,str2)
    print(p_str1);//hello~swift3.0
    
    
    
    p_str1 = String(format:"%@~%@-%d",str1,str2,456)
    
    print(p_str1);//hello~swift3.0-456
    
    //这种拼接方式方便地组合多种类型
    
    p_str1 = "\(str1)\(str2)\(456)"
    
    print(p_str1);//helloswift3.0456
    
    p_str1 = "\(str1)\(str2)\(char1)"
    
    print(p_str1);//helloswift3.0m
    
    //在后面添加
    p_str1.append(char1)
    print(p_str1);//helloswift3.0mm
    
    
    p_str1 += str2
    
    print(p_str1);//helloswift3.0mmswift3.0
    
    //与数组的组合
    var strArray = ["hello", "swift", "3.0"]
    
    p_str1 = strArray.joined(separator: "-")//数组通过指定字符拼接
    
    print("数组通过指定字符拼接\(p_str1)");
    
    
    strArray = p_str1.components(separatedBy: "-")//拆分为数组
    
    print("拆分为数组\(strArray)");
    
    print("p_str1字符串为:\(p_str1)");
    
    //枚举字符
    for ch in p_str1.characters{
        
        print(ch)
        
        switch ch {
        case "0":
            print("有")
        default:
            break
        }
    }
    

//3.0
/*
 获取字符串中指定索引的字符
 */

 //字符串长度
print("p_str1长度为:\(p_str1.characters.count)");

//首字母
char1 = p_str1[p_str1.startIndex]

print("首字母\(char1)");

//末字母
char1 = p_str1[p_str1.index(before: p_str1.endIndex)]

print("末字母\(char1)")

//第二个字母
char1 = p_str1[p_str1.index(after: p_str1.startIndex)]

print("第二个字母\(char1)")

//索引4的字母
char1 = p_str1[p_str1.index(p_str1.startIndex, offsetBy: 4)]

print("索引4的字母\(char1)")



//4.0
/*
 获取字符串子串
 */

let i = p_str1.index(p_str1.startIndex, offsetBy: 4)


let j = p_str1.index(p_str1.startIndex, offsetBy: 8)

//print(i,j);

var subStr = p_str1.substring(to: i)
print("截取到4\(subStr)")

subStr = p_str1.substring(from: i)
print("从索引4开始截取\(subStr)")

subStr = p_str1.substring(with: i..<j)
print("从索引4开始到8(不截取索引为8的字符)结束截取\(subStr)")

//通过扩展来简化一下
//  extension String {
//    
//    subscript (range: Range<Int>) -> String {
//        get {
//            let startIndex = self.index(self.startIndex, offsetBy: range.lowerBound)
//            let endIndex = self.index(self.startIndex, offsetBy: range.upperBound)
//            return self[Range(startIndex..<endIndex)]
//        }
//        
//        set {
//            let startIndex = self.index(self.startIndex, offsetBy: range.lowerBound)
//            let endIndex = self.index(self.startIndex, offsetBy: range.upperBound)
//            let strRange = Range(startIndex..<endIndex)
//            self.replaceSubrange(strRange, with: newValue)
//        }
//    }
//}

//subStr = p_str1[0..<14]


//通过指定字符串截取子串
let range1 = p_str1.range(of: "o-")

let range2 = p_str1.range(of: ".")

subStr = p_str1.substring(from: (range1?.upperBound)!)

print("从o-开始截取字符串:\(subStr)")

subStr = p_str1.substring(with: (range1?.upperBound)!..<(range2?.lowerBound)!)

print("从o-开始到.结束截取字符串:\(subStr)")

//5.0
/*
 插入、删除指定字符串
 */
subStr.insert("!", at: subStr.startIndex)

print(subStr);

subStr.insert("!", at: subStr.endIndex)

print(subStr);

subStr.insert(contentsOf:"YY".characters, at: subStr.index(after: subStr.startIndex))

print(subStr);

subStr.insert(contentsOf:"MM".characters, at: subStr.index(before: subStr.endIndex))

print(subStr)

let x = p_str1.index(p_str1.startIndex, offsetBy: 4)

//print(x)

subStr.remove(at: x)
print("删除索引为4的字符后得到的字符串\(subStr)")

subStr.remove(at: subStr.startIndex)
print("删除索引为0的字符后得到的字符串\(subStr)")

subStr.remove(at: subStr.index(after: subStr.startIndex))

print("删除索引为0的字符后得到的字符串\(subStr)")


subStr.remove(at: subStr.index(before: subStr.endIndex))
print("删除为最后一个索引的字符后得到的字符串\(subStr)")


let ran1 = subStr.index(subStr.endIndex, offsetBy: -3)..<subStr.endIndex

subStr.removeSubrange(ran1)

print("删除从索引为倒数第3之后所有的字符串后得到的字符串\(subStr)")

let ran2 = subStr.index(subStr.startIndex, offsetBy: 4)..<subStr.endIndex

subStr.removeSubrange(ran2)

print("删除从索引为4之后所有的字符串后得到的字符串\(subStr)")

subStr = "http://baidu.com"
let ran3 = subStr.range(of: ":")

let ran4 = subStr.range(of: ".")

subStr.removeSubrange((ran3?.upperBound)!..<(ran4?.lowerBound)!)
print("删除从:开始到.之间的字符串后得到的字符串\(subStr)");

//替换
subStr.replaceSubrange(ran3!, with: "wert")

print("ran3被wert替换之后的字符串\(subStr)");


//是否有前、后缀
p_str1.hasPrefix("3.0")

p_str1.hasSuffix("3.0")
//是否为空
p_str1.isEmpty
str3?.isEmpty
str4.isEmpty
str5.isEmpty

//6.0
/*
 其他
 */
//--- 大小写转换 -----------
subStr = "ashdfasdjf"

print("subStr.capitalized(首字母大写)方法:\(subStr.capitalized)")


print("subStr.uppercased()大写方法:\(subStr.uppercased())")


print("subStr.lowercased()小写方法:\(subStr.lowercased())")

//--- 字符串比较 -----------
subStr = "2.6.2"
p_str1 = "2.6.1"
str1 = "2.7"
str3 = "2.7.0"
subStr > p_str1
subStr > str1
str1 == str3
str1 > str3!
str1 < str3!

//--- 字符串转换 -----------
Int(str1)
Double(str1)
Double(str3!)
Bool(str1)
str1 = "true"
Bool(str1)

 类似资料: