0 votes

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)?

Godot version Godot_v3.4.4-stable
in Engine by (43 points)

1 Answer

+4 votes
Best answer

Hi! i think you could try this:

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.

by (81 points)
selected by

"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):

message = "AppleOrange"
insert_message = "X"
insert_position = 3
final_message = message.insert(insert_position,insert_message)
print(final_message)

But thanks again for your help!

happy to help, the important thing here that we all learned something. :D

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.