Topic was automatically imported from the old Question2Answer platform.
Asked By
kowid51384
For example, I’d like to turn “AppleOrange” into “AppXleOrange”. Can I insert a string in the middle of another string, or do I have to copy it into two different variables, erase some parts, and do something like this: print(varA + “X” + varB)?
var message: String = "AppleOrange" # the message
var indx: int = message.find('O') # we find the 'O' position
message = message.insert(indx,"Xle") # we insert the substring
# at the tokken's position
print(message) # AppleXleOrange
it worked for me. You can learn more in the String class documentation.
“message.insert()” was just what I was looking for, thanks!
Your answer is much more specific to my example than I was expecting, but I think that’s my fault, for posterity I was thinking something like this (inserting a string into another string after n number of characters):