To get a substring after a specific character in Swift, you can use the substring(from:)
method.
In this example,
str
with a string value.firstIndex(of:)
method.index(after:)
method to get the index right after the specific character.substring(from:)
method to get the substring starting from the calculated index.import Foundation
let str = "Hello,World"
if let index = str.firstIndex(of: ",") {
let substring = str[str.index(after: index)...]
print("Substring after comma is: \(substring)")
}
Substring after comma is: World
In this example,
str1
and str2
with different values.str1
using the firstIndex(of:)
method.index(after:)
method to get the index right after the specific character.substring(from:)
method to get the substring starting from the calculated index.import Foundation
let str1 = "Swift-Programming"
if let index = str1.firstIndex(of: "-") {
let substring = str1[str1.index(after: index)...]
print("Substring after hyphen is: \(substring)")
}
Substring after hyphen is: Programming
In this tutorial, we learned How to get Substring after Specific Character in Swift language with well detailed examples.