How to split a string character by character?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By inTech
:warning: Old Version Published before Godot 3 was released.

So I have a variable text where I got this input (String) from my users. I want to split the String character by character. I tried using the split method with blank divisor but it isn’t working. Below is a snippet of the code.

textArray = text.split("", true);

I tried “\0” as divisor but the engine say that it’s an invalid escape sequence.

Any work around?

Edit: It seem that I need to use RegEx with “(?!^)”. I am still trying to figure out how to use it. Any help will be appreciated.

:bust_in_silhouette: Reply From: mollusca

You can access the characters in a String like you would with a normal array:

var st = "abc"
print(st[0])

prints a.

Ahh. That was a nice trick. Thank you!

inTech | 2017-05-14 01:32

if you liked the answer, why not choosing it a the best answer ? it helps other people, trying to help.

MrMonk | 2017-05-16 14:53

Yep. I am sorry that I forgot about the “best answer” feature of Godot QA. :(.

Thanks for the reminder MrMonk.

inTech | 2017-05-17 11:38