Swift_字符串处理

//TODO:字符串
    func stringAp(){
        let a = 10
        let b = "trut"
        
        
        //转换
        var c = String(a)
        var e = String(a+a)+"tf"
        var d = "\(a)"
        var z = intptr_t("444")!

        //拼接
        var f = "I have \(a) apples."
        let g = "I have \(b + b) pieces of fruit."
        var h = d.appending(c);

        
        var myStr = "Hello"
        //查    字符串下标元字符
        var sub = myStr[myStr.startIndex] //0下标 T
        sub = myStr[myStr.index(before: myStr.endIndex)]   //之前一个
        sub = myStr[myStr.index(after: myStr.startIndex)]  //之后一个
        sub = myStr[myStr.index(myStr.startIndex, offsetBy: 2)]  //偏移量
        /*sub = myStr[myStr.endIndex] ;myStr[myStr.index(before: myStr.startIndex)] 这两种情况会越界 */
        
        
        //字符串截取
        let wef = myStr.substring(to: myStr.index(before: myStr.endIndex)) //截取从0下标到目标位置中间的字符串 Hello--Hell
        let wef2 = myStr.substring(from: myStr.index(after: myStr.startIndex)) //截取从包含目标下标开始到最后的字符串 Hello--ello
        let wef3 = myStr.substring(with: myStr.index(after: myStr.startIndex)..<myStr.index(myStr.endIndex, offsetBy: -2)) //去掉左右两端,剩下中间部分  Hello--el
        
        
        //增
        myStr.insert("!", at: myStr.endIndex) //Hello!
        myStr.insert(contentsOf: "Man".characters, at: myStr.endIndex) //Hello!Man
        myStr.insert(contentsOf: "!".characters, at: myStr.index(before: myStr.endIndex)) //Hello!Ma!n

        
        //删
        /*myStr.remove(at: myStr.endIndex) 越界*/
        myStr.remove(at: myStr.index(before: myStr.endIndex)) // Hello!Ma!
        let range = myStr.index(myStr.endIndex, offsetBy: -6)..<myStr.endIndex
        myStr.removeSubrange(range) //Hel
        
        
        //改
        myStr = "Hello"
        let newStr = myStr.replacingOccurrences(of: "el", with: "EL") //HEL
        let newStr2 = newStr.replacingOccurrences(of: "E", with: "e")  //HeL
        myStr.replaceSubrange(myStr.startIndex..<myStr.index(myStr.startIndex, offsetBy: 2), with: "dwe")
        myStr.replaceSubrange(myStr.startIndex..<myStr.index(myStr.startIndex, offsetBy: 2), with: "abc")
        myStr.replaceSubrange(myStr.startIndex..<myStr.index(myStr.startIndex, offsetBy: 2), with: "efg")
        var sdfeb=myStr.replacingCharacters(in: myStr.index(myStr.endIndex, offsetBy: -2)..<myStr.endIndex, with: "allo") // efgcello
        
        
        
        //是否为空字符串
        var boo:Bool = f.isEmpty
        boo = (boo as? Bool)!
        
        
        //Character 元字符
        let charac : Character? = "t"
        
        
        //遍历字符串每个characters
        for character in f.characters {
            
            print("通过遍历字符数组\(character)")
        }
        for index in f.characters.indices {
            
            print("通过下标\(f[index])")
        }
        
        
        
        
        //characters数组转String
        let character2:[Character] = ["v","w","c","e"]
        let str:String? = String(character2)
        
        
        
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值