swift之字符串的操作汇总

****创建空字符串



let str1: String = "gggggg"

let str2: String = String()

******创建赋初值的字符串



var str3 = "hello"

var str4 = String("你好")

*******判断字符串是否为空



if str.isEmpty{

    print("空值")

}

*********字符串长度

if  str.characters.count == 0 {

    print("空值")

}



************截取字符串



//字符串截取

let swiftString = "好喜欢!!!"

//将swiftString 转换为OC 的字符串使用,根据范围来截取字符串

let subString1 = (swiftString as NSString).substring(with: NSMakeRange(1, 3))

print(subString1)



//截取下标2 之前的字符

let subString2 = (swiftString as NSString).substring(to: 2)

print(subString2)

//截取下标2 之后(含有2)

let subString3 = (swiftString as NSString).substring(from: 2)

print(subString3)





********获取字符串中的指定字符



//swift语言中获取字符串的索引/下标 类型是Index类型 不是Int类型

//获取字符串中的第一个字符

print(str[str.startIndex])

//获取字符串中最后一个字符

//endIndex获取的是最后一个字符的下一位

//predecessor前一位

print(str[str.endIndex.predecessor()])

//successor后一位

print(str[str.startIndex.successor()])

//获取指定位上的字符

print(str[str.startIndex.advancedBy(6)])

******字符串的遍历



for i in str.character{

    print(i)

}



*********追加字符

var char: Character = "!"

str.append(char)

print(str)

//追加字符串

str.appendContentsOf("hkkhbhk")

print(str)

//使用加号追加字符串

str += "vvvvv"

print(str)

********插入字符串



//插入字符

str.insert(char, atIndex: str.startIndex.success())

//插入字符串

str.insertContentsOf("bkbuyhuy".characters,at: str.endIndex.predecessor())



//字符串最后

insertStr.insert("a", at: insertStr.endIndex)

print(insertStr)

//字符串最前

insertStr.insert("a", at: insertStr.startIndex)

print(insertStr)

//在指定位置插入

insertStr.insert("g", at: insertStr.index(before: insertStr.index(insertStr.startIndex, offsetBy: 3)))

print(insertStr)



************删除

//移除指定下标字符

insertStr.remove(at: insertStr.index(before: insertStr.index(insertStr.startIndex, offsetBy: 1)))



//删除指定位置的字符

str.removeAtIndex(str.startIndex)

//删除一定范围内的字符

str.removeRange(str.startIndex...str.sartIndex.advancedBy(5))

//删除所有

str3.removeAll()

**********修改字符串


str.replaceRange(str.startIndex...str.endIndex.predecessor(),with:"bukguyguyt")

********比较字符串是否相等



let string1 = "hello"

let string2 = "Hello"

if string1 == string2{

    print("相等")

}

*********字符和数字(asii码)之间相互转换



//字符转数字

let ch = "A"

var value = ch.unicodeScalars.first!.value

print(value)

//数字转字符

value += 1

var unicoder = UnicodeScalar.init(value)

let ch = Character.init(unicoder)

**********获取字符串的前后缀



//前缀

let path = "www.baidu.com"

path.hasPrefix("good")



//后缀

print(path.hasSuffix("com")

 

/**
 字符串的相关操作
 String.Index:表示在字符的CharacterView实例的位置,进入头文件就可以看到,其实是public typealias Index = String.CharacterView.Index。
 */

import UIKit

class LYBStringExtention: NSString {

    
        
}
extension String{
    
    func opString()
    {
        var str = "Hello Merry Christmas day!"
//        1:搜索字符串
        
        let range = str.range(of:"Hello") //正向检索
        print("\(range)")
        let backWardsRange = str.range(of:"Hello", options: .backwards)//反向检索
        print("\(backWardsRange)")
        let caseInsensitiveRange = str.range(of:"day", options: .caseInsensitive, range:nil , locale:nil)//忽略大小写
        print("\(caseInsensitiveRange)")
//        2:从字符串指定范围查找特定字符串
        
        //startIndex 第一个字符的位置
        let startIndex: String.Index = str.startIndex
        print("\( str[startIndex])")//H
        
        //endIndex 最后一个字符的位置
        let endIndex: String.Index = str.endIndex
      
        
        //after 给定位置之后的字符位置
        let afterIndex: String.Index = str.index(after:startIndex)
       print("\(str[afterIndex])") //e
        //range 指定范围
        let afterRange = str.index(after:startIndex)..<str.endIndex
 print("\(str[afterRange])")//ello Merry Christmas day!
        
        //before 给定位置之前的字符位置
        let beforeIndex: String.Index = str.index(before:str.endIndex)
     print("\( str[beforeIndex])")//!
        
        //offsetBy 偏移量,可以是正数或者负数
        //这里是以字符串开始位置为标准,向后偏移21个位置
        let offsetIndex: String.Index = str.index(str.startIndex, offsetBy:12)
    print("\(str[offsetIndex])") //C
        let offsetRange = startIndex ..< offsetIndex
      print("\( str[offsetRange])") //Hello Merry
        
        //limitedBy 给定限制范围,确保不会越界
        if let index = str.index(str.startIndex, offsetBy:10, limitedBy: str.endIndex){
          print("\(str[index])")  //y
        }
        
        let subStringRange = str.range(of:"Merry", options: .caseInsensitive, range:offsetRange)
        print(subStringRange)
        
        //distance 查找字符的位置
        let string = "Hello.World"
        let needle: Character =  "."
        if let idx = string.index(of:needle) {
            let pos = string.distance(from:string.startIndex, to: idx)
            print("Found\(needle) at position\(pos)")//Found . at position 5
        }
        else {
            print("Not found")
        }
        
//        3:截取子串
       
        let subIndex: String.Index = str.index(str.startIndex, offsetBy:12)
        
      
        //注意:对字符串进行截取操作,原来的字符串不变 Hello Merry Christmas day!
        
//        4:插入字符到字符串
      
        str.insert("L", at:startIndex)//LHello Merry Christmas day!
//        5:末尾追加字符串
        
     
        str.append("A Apple day!")//LHello Merry Christmas day!A Apple day!
        
//        6:使用字符串替换指定范围的子字符串
        str.replaceSubrange(str.startIndex..<str.index(str.startIndex, offsetBy: 6), with:"Hello") //"Hello Merry Christmas day!A Apple day!
        
        
//        7:去除字符
        str.remove(at:startIndex) //去除指定位置的字符
        str.removeSubrange(subIndex..<endIndex)//去除指定范围的字符
        str.removeAll()//去除所有
     
        
    }
    
  
}

字符串:https://www.jianshu.com/p/dcf6b52aa420

Range:https://www.jianshu.com/p/a0f3556169ba 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值