美文网首页
Swift基础篇——字符串

Swift基础篇——字符串

作者: 沫简影 | 来源:发表于2016-10-26 22:55 被阅读139次

    字符

    正式介绍字符串之前呢,需要引入一个新的数据类型,即字符(Character),字符类型只能存放一个字符,例如:

    var ch : Character = "!"
    

    单独的一个字符可以拼接在一个字符串的后面,具体方法如下:

    var str = "Hello, playground"
    str.append(ch)
    

    字符串

    //常量字符串,不能对其进行重新赋值或者拼接等操作
    let str2 = "I'm an immutable string"
    
    //可变字符串
    var str = "你好, playground"
    //字符串的拼接
    str += "\u{1F496}"
    
    //初始化一个空的字符串
    //方法一
    var emptyString = ""
    //方法二    实际上是创建了一个字符串类的对象
    var anotherEmptyString = String()
    
    //判断当前字符串是否为空,返回的是一个Bool类型
    str.isEmpty
    
    //字符串的遍历
    for c in str.characters
    {
        print(c)
    }
    
    //计算字符串的长度
    str.characters.count
    
    //字符串的插值 String Interpolation
    let imInt:Int = 2
    let imTouble:(Bool,Float) = (true,1.1435926)
    print("\(imInt),\(imTouble.0)")
    //输出结果为:2,true
    
    //字符串的比较
    let str_a = "abc"
    let str_b = "abc"
    str_a == str_b //true
    let str_c = "abd"
    str_a < str_c  //true
    let str_d = "abcd"
    str_c < str_d //false
    
    //前缀和后缀的判断
    str.hasPrefix("http") //false
    str.hasSuffix("playground") //true
    
    //import Foundation以后就可以用String来使用OC中NSString中的所有方法
    import Foundation
    
    // 下面的大小写转换的方法均不改变str本身的值
    //把字符串中的每一个单词的首字母都转换成大写
    str.capitalized
    //把字符串中所有的字符转换成大写
    str.uppercased()
    //把字符串
    str.lowercased()
    
    //trim操作,即对字符串头尾中没有用的字符进行截取,只保留中间有用的部分
    var str3 = "      !so cool        "
    str3.trimmingCharacters(in: NSCharacterSet.whitespaces) //!so cool
    str3.trimmingCharacters(in: NSCharacterSet.init(charactersIn: " !") as CharacterSet)//so cool
    
    //字符串的分割:把字符串按某个字符或字符串截取成一个一个的单词
    var str4 = "welcom to play"
    str4.components(separatedBy: " ") //["welcom", "to", "play"]
    str4 = "welcoom to play swift!Step-by-step learn from now"
    str4.components(separatedBy: NSCharacterSet.init(charactersIn: " !-") as CharacterSet)//["welcoom", "to", "play", "swift", "Step", "by", "step", "learn", "from", "now"]
    
    //join 字符串的连接
    var arry = ["a","b","c"]
    let res = arry.joined(separator: "_") //"a_b_c"
    
    
    //查找字符串中某个字符穿出现的范围
    str4.range(of:"play")
    str4.range(of: "welcom", options: String.CompareOptions.caseInsensitive)//忽略大小写进行查找
    
    str4.startIndex
    str4.endIndex
    
    //截取字符串
    let startIndex:String.Index = str4.startIndex
    let endIndex:String.Index = str4.index(str4.startIndex, offsetBy: 8)
    //下面的写法在swift 3.0中是错误的
    //let strRange = Range<String.Index>(start:startIndex,end:endIndex)
    //swift 3.0中的正确写法为
    let strRange = startIndex ..< endIndex
    str4.substring(with: strRange) //"welcome t"
    str4.substring(to: endIndex) //"welcome t"
    str4.substring(from: startIndex)
    
    //插入字符串
    var insertIndex = str4.index(str4.startIndex, offsetBy: 2)
    str4.insert("!", at: insertIndex)
    
    //删除字符
    str4.remove(at: insertIndex)
    str4.removeSubrange(strRange)
    
    //替换字符串
    str4.replaceSubrange(strRange, with: "hello")
    

    关于字符串的更多操作后续补充,可参考这里

    相关文章

      网友评论

          本文标题:Swift基础篇——字符串

          本文链接:https://www.haomeiwen.com/subject/ryyhuttx.html