let colors =
"blue,red,green"
// Get character indexes.
let indexE = colors.
index(of:
"e")!
let indexComma = colors.
index(of:
",")!
// Get before and after indexes.
let indexBeforeE = colors.
index(before: indexE)
let indexAfterE = colors.
index(after: indexE)
let indexBeforeComma = colors.
index(before: indexComma)
let indexAfterComma = colors.
index(after: indexComma)
// Get substrings based on ranges.
print("IndexE: \(colors[indexE..<colors.endIndex])")
print("IndexComma: \(colors[indexComma..<colors.endIndex])")
print("IndexBeforeE: \(colors[indexBeforeE..<colors.endIndex])")
print("IndexAfterE: \(colors[indexAfterE..<colors.endIndex])")
print("IndexBeforeComma: \(colors[indexBeforeComma..<colors.endIndex])")
print("IndexAfterComma: \(colors[indexAfterComma..<colors.endIndex])")
IndexE: e,red,green
IndexComma: ,red,green
IndexBeforeE: ue,red,green
IndexAfterE: ,red,green
IndexBeforeComma: e,red,green
IndexAfterComma: red,green