Slice a string from a Dictionnary letter by letter

Hey, I have a question, How I can Slice this —>

{

      [1] = "CoolWord!",
      [2] = "SecondCoolWord!"

}

To this :

*Output :* 

C
O
O
L
W
O
R
D
!
1 Like
local function getLetters(word: string): {string}
	return word:split("")
end

local dict = {"CoolWord!", "SecondCoolWord!"}

print("*Output: *\n")
for i, word in pairs(dict) do
	for _, letter in pairs(getLetters(word)) do
		print(letter:upper()) --since you want them in uppercase
	end
	--you can comment/remove this if you don't want any text between them
	print("End word "..i.."("..word..")")
end

Alternatively, you can use string.sub.

local str = “hello”
for i = 1, string.len(str) do
 print(string.sub(str,i,i))
end

Edited to fix mistake.

You forgot to add the word itself as an argument to string.sub().


Here’s a slightly different bit of code that uses string.sub():

local dict = {"CoolWord!", "SecondCoolWord!"}

for Index, Word in pairs(dict) do
    for i = 1, #Word do
    	print(string.sub(Word, i, i):upper())
    end
    print("\n\n\n") -- just a seperator between the words. 
end
1 Like