swift3 : 문자열 substring 과 replace

2016. 10. 28. 14:10Programming/Swift

반응형

var str = "Hello, playground"


 * 앞자리 기준

 - 0번째 자리 부터

let start = str.index(str.startIndex, offsetBy: 0

str.substring(from: start)  //Hello, playground 출력


 - 2번째 자리 부터

  => "offsetBy: 2"의 숫자 2는 2번째 자리가 아니라 3번째 자리이다.

       index는 0부터 시작하므로, 즉 3자리미만으로 해석해야함.

let end = str.index(str.startIndex, offsetBy: 2)

str.substring(from: end)  //llo, playground 출력


 * 뒷자리 기준

 - 뒤에서 4번째 자리 부터

let end = str.index(str.endIndex, offsetBy: -4)

str.substring(from: end)  //ound 출력



 * substring

 - 1번째 자리부터 2자리만 가지고 온다.

let start2 = str.index(str.startIndex, offsetBy: 2)

let range = start ..< start2

str.substring(with: range)  //He 출력


 - 3번째 자리부터 끝에서 4번째자리 이전까지만 가지고 온다.

let range2 = start ..< end

str.substring(with: range2)  //llo, playgr 출력


var replaceStr = "This is my string"


 * replace

let NewString = replaceStr.replacingOccurrences(of: " ", with: "+")

print(NewString) //This+is+my+string



종합하여, 해당 범위에 있는 문자를 추출하고자 한다면...

예시로 "my"를 "your"로 바꾸어 출력해 보겠다.

let begin = replaceStr.index(replaceStr.startIndex, offsetBy: 8)

let finish = replaceStr.index(replaceStr.endIndex, offsetBy: -7)

let NewRange = begin ..< finish

let StringNew = replaceStr.replacingOccurrences(of: replaceStr.substring(with: NewRange), with: "your")

print(StringNew)  //This is your string 출력



playground

//: Playground - noun: a place where people can play


import UIKit


let channel = "asdfg35sahadhxbadgaswgreh"



let start5 = channel.index(channel.startIndex, offsetBy: 0)

let end5 = channel.index(channel.startIndex, offsetBy: 2)

let range5 = start5 ..< end5

channel.substring(with: range5)  // play

let name5 = channel.replacingOccurrences(of: channel.substring(with: range5), with: "UU")


var str = "Hello, playground"

let start = str.index(str.startIndex, offsetBy: 0)

str.substring(from: start)


let start2 = str.index(str.startIndex, offsetBy: 2)

str.substring(from: start2)


let end = str.index(str.endIndex, offsetBy: -4)

str.substring(from: end)



let range = start ..< start2

str.substring(with: range)


let range2 = start2 ..< end

str.substring(with: range2)


let replaceStr = "This is my string"

let NewString = replaceStr.replacingOccurrences(of: " ", with: "+")

print(NewString)


let begin = replaceStr.index(replaceStr.startIndex, offsetBy: 8)

replaceStr.substring(from: begin)

let finish = replaceStr.index(replaceStr.endIndex, offsetBy: -7)

replaceStr.substring(from: finish)

let NewRange = begin ..< finish

replaceStr.substring(with: NewRange)

let name = replaceStr.replacingOccurrences(of: replaceStr.substring(with: NewRange), with: "your")

print(name)




let ins = "select a from name"

//ins.indexes(of: " ")

var rang = ins.range(of: " ")

var aaa = ins.indexes(of: " ")


print(aaa[1])


extension String {

    func index(of string: String, options: String.CompareOptions = .literal) -> String.Index? {

        return range(of: string, options: options)?.lowerBound

    }

    func indexes(of string: String, options: String.CompareOptions = .literal) -> [String.Index] {

        var result: [String.Index] = []

        var start = startIndex

        while let range = range(of: string, options: options, range: start..<endIndex) {

            result.append(range.lowerBound)

            start = range.upperBound

        }

        return result

    }

    func ranges(of string: String, options: String.CompareOptions = .literal) -> [Range<String.Index>] {

        var result: [Range<String.Index>] = []

        var start = startIndex

        while let range = range(of: string, options: options, range: start..<endIndex) {

            result.append(range)

            start = range.upperBound

        }

        return result

    }

}


반응형