Topic was automatically imported from the old Question2Answer platform.
Asked By
brunosxs
Old Version
Published before Godot 3 was released.
Hello guys. I have a problem that has been bugging me. Supose I have the following:
var strings = ["Hello ", "there!", " How'dy?"]
I’ve been trying to find a good way to create a String from this variable in an elegant manner.
The only way I was able to, involved me using var complete = String(strings)but this in turn generated a written representation of the array, with the brackets and commas.
Its a little perplexing that theres a function to do this in one direction (String.rsplit) but not in the other direction.
For anyone that finds this and also wants to be able to join the items using an additional string as the “glue” (like a comma, for example), this works well:
func array_join(arr : Array, glue : String = '') -> String:
var string : String = ''
for index in range(0, arr.size()):
string += str(arr[index])
if index < arr.size() - 1:
string += glue
return string
if you’re using godot version < 3.1:
func array_join(arr, glue = ''):
var string = ''
for index in range(0, arr.size()):
string += str(arr[index])
if index < arr.size() - 1:
string += glue
return string