0

I want to know how to get rest of string after specific character.

I have checked link

but have not found answer.

Consider following code:

let fullString = "Blue Sky"
let spaceIndex = fullName.index(of: " ")!

I know i can get first string before like that:

let firstString = fullString[fullString.startIndex..<spaceIndex] // "Blue”

But let firstString = fullString[fullString.startIndex..>spaceIndex] // "Blue” not work.

What i want is - "Sky". How to get it?

Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107

3 Answers3

7

You can separate the sentence into array of words and get last word. Try this. You can replace " " with any character or string.

let fullString = "Blue Sky"
print(fullString.components(separatedBy: " ").last)//Sky

Or

if let index = fullString.firstIndex(of: " ") {
    print(fullString[index...])//Sky
}
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
3

components(separatedBy:) will split your string into an array of strings enabling you to access the different elements in the array using indexes.

 let fullString = "Blue Sky"    
 let splitString = fullString.components(separatedBy: " ")

 print("Part before space: \(splitString[0])") // Part before space: Blue
 print("Part after space: \(splitString[1])") // Part after space: Sky
matiastofteby
  • 431
  • 4
  • 18
2

There is no such operator as ..> in Swift.

However, you can use the ..< operator, as in fullString[spaceIndex..<fullString.endIndex].

Or, in Swift 4, you can just: fullString[spaceIndex...].

Charles Srstka
  • 16,665
  • 3
  • 34
  • 60