Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. let str = "Hello, playground"
  2. let prefixRange = str.startIndex..<str.startIndex.advancedBy(5)
  3. let prefix = str.substringWithRange(prefixRange)
  4.  
  5. str.substring(to: String.Index)
  6. str.substring(from: String.Index)
  7. str.substring(with: Range<String.Index>)
  8.  
  9. var str = "Hello, playground"
  10.  
  11. let index = str.index(str.startIndex, offsetBy: 5)
  12. str.substring(to: index) // Hello
  13.  
  14. let index = str.index(str.startIndex, offsetBy: 7)
  15. str.substring(from: index) // playground
  16.  
  17. let start = str.index(str.startIndex, offsetBy: 7)
  18. let end = str.index(str.endIndex, offsetBy: -6)
  19. let range = start..<end
  20.  
  21. str.substring(with: range) // play
  22.  
  23. for i in 0..<5 {
  24. print(s[i])
  25. }
  26.  
  27. extension String {
  28. func index(from: Int) -> Index {
  29. return self.index(startIndex, offsetBy: from)
  30. }
  31.  
  32. func substring(from: Int) -> String {
  33. let fromIndex = index(from: from)
  34. return substring(from: fromIndex)
  35. }
  36.  
  37. func substring(to: Int) -> String {
  38. let toIndex = index(from: to)
  39. return substring(to: toIndex)
  40. }
  41.  
  42. func substring(with r: Range<Int>) -> String {
  43. let startIndex = index(from: r.lowerBound)
  44. let endIndex = index(from: r.upperBound)
  45. return substring(with: startIndex..<endIndex)
  46. }
  47. }
  48.  
  49. let str = "Hello, playground"
  50. print(str.substring(from: 7)) // playground
  51. print(str.substring(to: 5)) // Hello
  52. print(str.substring(with: 7..<11)) // play
  53.  
  54. let rString = cString.substringToIndex(2)
  55. let gString = (cString.substringFromIndex(2) as NSString).substringToIndex(2)
  56. let bString = (cString.substringFromIndex(4) as NSString).substringToIndex(2)
  57.  
  58. let rString = String(cString.characters.prefix(2))
  59. cString = String(cString.characters.dropFirst(2))
  60. let gString = String(cString.characters.prefix(2))
  61. cString = String(cString.characters.dropFirst(2))
  62. let bString = String(cString.characters.prefix(2))
  63.  
  64. let s = "abcdefghi"
  65. let i = 2
  66. print (s[s.index(s.startIndex, offsetBy:i)])
  67. // print c
  68.  
  69. let f = 6
  70. print (s[s.index(s.startIndex, offsetBy:i )..<s.index(s.startIndex, offsetBy:f+1 )])
  71. //print cdefg
  72.  
  73. print (s.substring (with:s.index(s.startIndex, offsetBy:i )..<s.index(s.startIndex, offsetBy:f+1 ) ) )
  74. //print cdefg
  75.  
  76. //
  77. // Play with finding substrings returning an array of the non-unique words and positions in text
  78. //
  79. //
  80.  
  81. import UIKit
  82.  
  83. let Bigstring = "Why is it so hard to find substrings in Swift3"
  84. let searchStrs : Array<String>? = ["Why", "substrings", "Swift3"]
  85.  
  86. FindSubString(inputStr: Bigstring, subStrings: searchStrs)
  87.  
  88.  
  89. func FindSubString(inputStr : String, subStrings: Array<String>?) -> Array<(String, Int, Int)> {
  90. var resultArray : Array<(String, Int, Int)> = []
  91. for i: Int in 0...(subStrings?.count)!-1 {
  92. if inputStr.contains((subStrings?[i])!) {
  93. let range: Range<String.Index> = inputStr.range(of: subStrings![i])!
  94. let lPos = inputStr.distance(from: inputStr.startIndex, to: range.lowerBound)
  95. let uPos = inputStr.distance(from: inputStr.startIndex, to: range.upperBound)
  96. let element = ((subStrings?[i])! as String, lPos, uPos)
  97. resultArray.append(element)
  98. }
  99. }
  100. for words in resultArray {
  101. print(words)
  102. }
  103. return resultArray
  104. }
  105.  
  106. extension String {
  107. func substring(from: Int, length: Int) -> String? {
  108. guard characters.count >= from + length else { return nil }
  109. let start = index(startIndex, offsetBy: from)
  110. let end = index(startIndex, offsetBy: from + length)
  111. return substring(with: start..<end)
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement