Split string every n characters, and store into an array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ryanrom5

Is there a way to split a string every n characters and store the results into an array? I attempted, but I’m new to GDscript. Thank you.

var text: String
var i = 0
var lines =
for line in range(text[0], text.length(), 100):
lines[i] = line
i++

:bust_in_silhouette: Reply From: rossunger

String has a function called left() which gives you the first x characters of that string and also a substr function which takes a parameter indicating how many characters in to start, and a parameter indicating how many character to take, and returns a string of those character:

var text = "THIS IS YOUR STRING"
var arr = [ ]
var i = offset
while i * 5 < text.length():
  arr.push_back( text.substr(i*5, 5) )
  i += 1