-3

I have following Obj-c statement:

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

What it's Swift equivalent?

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Alexey K
  • 6,537
  • 18
  • 60
  • 118

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
  • 30,560
  • 17
  • 97
  • 143
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,342
  • 1
  • 38
  • 37