-3

I have following Obj-c statement:

string = [string substringFromIndex:(range.location + range.length)];

What it's Swift equivalent?

Ahmad F
  • 28,447
  • 13
  • 87
  • 133
Alexey K
  • 6,159
  • 18
  • 55
  • 109

2 Answers2

0

You should use substring(from:):

var string = "Hello World"

string = string.substring(from: string.index(string.startIndex, offsetBy: 6))

print(string) // "World"

Also, you might want to use substring(to:):

let hello = string.substring(to: string.index(string.startIndex, offsetBy: 5))

print(hello) // "Hello"

For more information about String.Index, check this question/answer.

Community
  • 1
  • 1
Ahmad F
  • 28,447
  • 13
  • 87
  • 133
0

If you do not want to use substring:

let string = "abcdefghijklm"
let startPoint = 2
let length = 5

let startIndex = string.index(string.startIndex, offsetBy: startPoint)
let endIndex = string.index(startIndex, offsetBy: length)

let result = string[startIndex ..< endIndex]
print(result)         // cdefg
Vincent
  • 4,248
  • 1
  • 37
  • 37