Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let str = "Hello, playground"
- let prefixRange = str.startIndex..<str.startIndex.advancedBy(5)
- let prefix = str.substringWithRange(prefixRange)
- str.substring(to: String.Index)
- str.substring(from: String.Index)
- str.substring(with: Range<String.Index>)
- var str = "Hello, playground"
- let index = str.index(str.startIndex, offsetBy: 5)
- str.substring(to: index) // Hello
- let index = str.index(str.startIndex, offsetBy: 7)
- str.substring(from: index) // playground
- let start = str.index(str.startIndex, offsetBy: 7)
- let end = str.index(str.endIndex, offsetBy: -6)
- let range = start..<end
- str.substring(with: range) // play
- for i in 0..<5 {
- print(s[i])
- }
- extension String {
- func index(from: Int) -> Index {
- return self.index(startIndex, offsetBy: from)
- }
- func substring(from: Int) -> String {
- let fromIndex = index(from: from)
- return substring(from: fromIndex)
- }
- func substring(to: Int) -> String {
- let toIndex = index(from: to)
- return substring(to: toIndex)
- }
- func substring(with r: Range<Int>) -> String {
- let startIndex = index(from: r.lowerBound)
- let endIndex = index(from: r.upperBound)
- return substring(with: startIndex..<endIndex)
- }
- }
- let str = "Hello, playground"
- print(str.substring(from: 7)) // playground
- print(str.substring(to: 5)) // Hello
- print(str.substring(with: 7..<11)) // play
- let rString = cString.substringToIndex(2)
- let gString = (cString.substringFromIndex(2) as NSString).substringToIndex(2)
- let bString = (cString.substringFromIndex(4) as NSString).substringToIndex(2)
- let rString = String(cString.characters.prefix(2))
- cString = String(cString.characters.dropFirst(2))
- let gString = String(cString.characters.prefix(2))
- cString = String(cString.characters.dropFirst(2))
- let bString = String(cString.characters.prefix(2))
- let s = "abcdefghi"
- let i = 2
- print (s[s.index(s.startIndex, offsetBy:i)])
- // print c
- let f = 6
- print (s[s.index(s.startIndex, offsetBy:i )..<s.index(s.startIndex, offsetBy:f+1 )])
- //print cdefg
- print (s.substring (with:s.index(s.startIndex, offsetBy:i )..<s.index(s.startIndex, offsetBy:f+1 ) ) )
- //print cdefg
- //
- // Play with finding substrings returning an array of the non-unique words and positions in text
- //
- //
- import UIKit
- let Bigstring = "Why is it so hard to find substrings in Swift3"
- let searchStrs : Array<String>? = ["Why", "substrings", "Swift3"]
- FindSubString(inputStr: Bigstring, subStrings: searchStrs)
- func FindSubString(inputStr : String, subStrings: Array<String>?) -> Array<(String, Int, Int)> {
- var resultArray : Array<(String, Int, Int)> = []
- for i: Int in 0...(subStrings?.count)!-1 {
- if inputStr.contains((subStrings?[i])!) {
- let range: Range<String.Index> = inputStr.range(of: subStrings![i])!
- let lPos = inputStr.distance(from: inputStr.startIndex, to: range.lowerBound)
- let uPos = inputStr.distance(from: inputStr.startIndex, to: range.upperBound)
- let element = ((subStrings?[i])! as String, lPos, uPos)
- resultArray.append(element)
- }
- }
- for words in resultArray {
- print(words)
- }
- return resultArray
- }
- extension String {
- func substring(from: Int, length: Int) -> String? {
- guard characters.count >= from + length else { return nil }
- let start = index(startIndex, offsetBy: from)
- let end = index(startIndex, offsetBy: from + length)
- return substring(with: start..<end)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement