About the endInex

at this sentence: "welcome.insert(contentsOf: " there", at: welcome.index(before: welcome.endIndex))"
before the end index of welcome is "!", why it cannot show "welcome" print out "hello! there"?Thx

"endIndex" points right after the last character. If you want to insert after "!" in "hello!" use welcome.endIndex instead of the index before it. You may as well use append() if you want to insert at the end.

BTW, code fragments are better posted in the original text form within triple ``` (backtick) brackets:

var welcome = "hello"
1 Like