美文网首页
Swift截取字符串

Swift截取字符串

作者: 山有木枝壮 | 来源:发表于2017-02-05 17:19 被阅读114次
一、Swift字符串操作,常用的一些方法,获取字符串长度,字符串首字母大写,字符串全部大写等

<pre>
/// 一些常用string方法
func otherStringOperator() {
var originStr = "Hello swift a,w 'ss @#'skk.kk"
print(originStr.characters.count)
print(originStr.contains("origin"))

print(originStr.uppercased())
print(originStr.capitalized)        // 单词首字母大写,空格 , .为首字母大写依据

print(originStr)

}
</pre>

二、截取字符串

swift截取子串主要有三个方法,到某个位置结束、从某个位置开始,或者指定开始和结束的位置,其中需要用到String.Index。这个index代表一个位置,一般是从某个位置偏移多少。如果要获取首个和最后一个位置,可以使用endIndex和startIndex
<pre>
// 截取字符串
func doSubString() {
let originStr = "this is an origin string,I want get some substring form there"

print("到某个位置结束或者从某个位置开始")
let start1 = originStr.index(originStr.startIndex, offsetBy: 5)
let index1 = originStr.index(start1, offsetBy: 4)
let sub1 = originStr.substring(to: index1)          // 从开始到某个位置
let sub11 = originStr.substring(from: index1)       // 从某个位置到结束
print(sub1)
print(sub11)

print("字符串分割")
let sub2 = originStr.components(separatedBy: ",")
for item in sub2 {
    print(item)
}

if let index3 = originStr.index(originStr.startIndex, offsetBy: 4, limitedBy: originStr.endIndex) {
    let sub3 = originStr.substring(to: index3)
    print(sub3)
}

print("截取指定位置、指定长度的字符串")
let start = originStr.index(originStr.startIndex, offsetBy: 11)

// let end = originStr.index(originStr.startIndex, offsetBy: 23)
let end = originStr.index(start, offsetBy: 13) // 从开始位置、偏移量,获取的是一个下标
if start <= end {
let range = Range(uncheckedBounds: (start, end))
let sub4 = originStr.substring(with: range)
print(sub4)
} else {
print("start must <= end")
}

}
</pre>

三、字符串替换

前面的index和Range如果弄清楚的话,字符串替换会比较容易理解。字符串替换可以自己指定一个Range,然后替换成对应的字符串,也可以先在原来的字符串中查找一个range,然后进行替换
<pre>
/// 字符串替换
func doStringReplace() {
var originStr = "this is a origin string,I want get some substring form there"

if let range = originStr.range(of: ",") {
    let str1 = originStr.replacingCharacters(in: range, with: ", ")
    print(str1)
    
    let str2 = originStr.replacingOccurrences(of: " ", with: "-")
    print(str2)
    
    let str3 = originStr.remove(at: range.upperBound)
    print(str3)
    
    originStr.removeSubrange(range)
    
}

print(originStr)

}
</pre>

四、附上一个字符串的扩展

可以根据开始位置,结束位置,或者开始位置,指定长度获取子串,有些类似java中获取字符串的方法。有需要的话,还可以扩展传负数的情况,从字符串的末尾截取,但是使用上面的两个方法可以进行变通。
<pre>
extension String {
/// 从指定位置截取指定长度字符串
func substring(from start: Int, length: Int) -> String? {
if start > self.characters.count {
print("起始位置过长")
return nil
}

    if start + length > self.characters.count {
        print("截取位置错误")
        return nil
    }
    
    if start < 0 || length < 0 {
        return nil
    }
    
    if length == 0 {
        return nil
    }
    
    let startIndex = self.index(self.startIndex, offsetBy: start)
    let endIndex = self.index(startIndex, offsetBy: length)
    return self.substring(with: Range(uncheckedBounds: (startIndex, endIndex)))
}

/// 指定开始位置和结束位置
func substring(from start: Int = 0, end: Int = 0) -> String? {
    let length = end - start
    return self.substring(from: start, length: length)
}

}

func testSubString() {
let origin = "hello world!"
print(origin.characters.count)
print(origin.endIndex)

if let sub = origin.substring(from: 0, length: 10) {
    print(sub)
}

guard let sub = origin.substring(from: origin.characters.count - 6, end: origin.characters.count) else {
    print("截取出错")
    return
}

print(sub)

}

testSubString()
</pre>

上面的方法可以直接复制到PlayGround中运行,可以修改对应的参数查看效果

相关文章

网友评论

      本文标题:Swift截取字符串

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