swift 3.0 的字符串用法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 3.0 的字符串用法相关的知识,希望对你有一定的参考价值。

参考技术A

// 取得字符串数字下标的方法
func index(str: String, subIndex: Int) -> String

let num = index(str: str, subIndex: 0)

print(num)

// 简化版为
var str = "12345678"

func index(str: String, subIndex: Int) -> String

let num = index(str: str, subIndex: 0)

print(num)

//------------------

var string = "Hello-Swift"
//获取某个下标后一个下标对应的字符 char="e"
//swift2.2
//var char = string[startIndex.successor()]
//swift3.0
var char = string[string.index(after: startIndex)]
//获取某个下标前一个下标对应的字符 char2 = "t"
//swift2.2
//var char2 = string[endIndex.predecessor()]
//swift3.0
var char2 = string[string.index(before: string.endIndex)]
//通过范围获取字符串中的一个子串 Hello
//swift2.2
//var subString = string[startIndex...startIndex.advancedBy(4)]
//swift3.0
var subString = string[startIndex...string.index(startIndex, offsetBy: 4)]
//swift2.2
//var subString2 = string[endIndex.advancedBy(-5)...endIndex.predecessor()]
//swift3.0
var subString2 = string[string.index(endIndex, offsetBy: -5)..<endIndex]
//获取某个子串在父串中的范围
//swift2.2
//var range = string.rangeOfString("Hello")
//swift3.0
var range = string.range(of: "Hello")
//追加字符串操作 此时string = "Hello-Swift! Hello-World"
//swift2.2
//string.appendContentsOf(" Hello-World")
//swift3.0
string.append(" Hello-World")
//在指定位置插入一个字符 此时string = "Hello-Swift!~ Hello-World"
//swift2.2
//string.insert("~", atIndex: string.startIndex.advancedBy(12))
//swift3.0
string.insert("~", at: string.index(string.startIndex, offsetBy: 12))
//在指定位置插入一组字符 此时string = "Hello-Swift!~~~~ Hello-World"
//swift2.2
//string.insertContentsOf([" "," ","~"], at: string.startIndex.advancedBy(12))
//swift3.0
string.insert(contentsOf: [" "," ","~"], at: string.index(string.startIndex, offsetBy: 12))
//在指定范围替换一个字符串 此时string = "Hi-Swift!~~~~ Hello-World"
//swift2.2
//string.replaceRange(string.startIndex...string.startIndex.advancedBy(4), with: "Hi")
//swift3.0
string.replaceSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 4), with: "Hi")
//在指定位置删除一个字符 此时string = "Hi-Swift!~~~~ Hello-Worl"
//swift2.2
//string.removeAtIndex(string.endIndex.predecessor())
//swift3.0
string.remove(at: string.index(before:string.endIndex))
//删除指定范围的字符 此时string = "Swift!~~~~ Hello-Worl"
//swift2.2
//string.removeRange(string.startIndex...string.startIndex.advancedBy(2))
//swift3.0
string.removeSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 2))
var string2 = "My name is Jaki"
//全部转换为大写
//swift2.2
//string2 = string2.uppercaseString
//swift3.0
string2 = string2.uppercased()
//全部转换为小写
//swift2.2
//string2 = string2.lowercaseString
//swift3.0
string2 = string2.lowercased()

swift Swift Zip功能用法

//Zip function (Sequence)
// https://github.com/apple/swift/blob/master/stdlib/public/core/Zip.swift
// https://developer.apple.com/reference/swift/1541125-zip
// http://swiftdoc.org/v3.0/func/zip/

let array1 = ["one", "two", "three", "four", "five"]
let array2 = 1...5

let zipSequence = zip(array1, array2)
type(of: zipSequence)   //Zip2Sequence<Array<String>, CountableClosedRange<Int>>.Type

zipSequence.forEach { (key, value) in
    print("key: \(key), value: \(value)")
}

//key: one, value: 1
//key: two, value: 2
//key: three, value: 3
//key: four, value: 4
//key: five, value: 5

以上是关于swift 3.0 的字符串用法的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3.0 字符串问题

使用 Swift 3.0 将包含“字符串数组”的字符串解析为数组 [重复]

在swift 3.0中将坐标数组转换为geojson字符串

无法在 swift 3.0 中转换正确的设备令牌

如何使用 Swift 3.0 解析这个 JSON 对象

Swift 3.0,Firebase:如何获取最后添加的记录