# ComputerCraft | Programmable Computers for Minecraft

»

## MrabEzreb19 Feb 2014

I have tried the CC wiki AND the LUA documentation, but I STILL AM SO CONFUSED! (please exuse my rage)
I need a way to take a string and break it into letters. Example:
```bVar = "ABCD"
eVar = crazyFunc(bVar)
eVar = {"A", "B", "C", "D"}
```
or something like that. But I just can't find anything! I think that it would somehow use string.gsub(), but I can't figure out how it would work! PLEASE help me!

## Kingdaro19 Feb 2014

What you would do is create a new table, go through each letter in your string, then insert each letter one by one into the new table.
```word = "ABCD"
letters = {}

for i=1, string.len(word) do -- loop, start at 1, go to the length of "word"
letters[i] = string.sub(word, i, i) -- in each loop, insert the ith letter into the letters table
end
```

## MrabEzreb19 Feb 2014

OKAY! So...
```word = string
start = number
end = number
string.sub(word, start, end)
```
that is how string.sub() works?

## MrabEzreb19 Feb 2014

so the function is:
```function breakUp(text, size) --Size is how many letters the chunks are
if size == nil then
size = 1
end
for i = 1, string.len(text), size do
broken[i] = string.sub(text, i, i + size - 1)
end
return broken
end
```
And that would work?
Edited by MrabEzreb, 19 February 2014 - 01:06 PM.

## MrabEzreb19 Feb 2014

Would it also work on numbers?
```letters = breakUp("ABCD")
numbers = breakUp("1234")
letters = {"A", "B", "C", "D"}
numbers = {"1", "2", "3", "4"}
```

## CometWolf19 Feb 2014

string.len returns the string size, so you don't need to input that, but yeah it would work on all characters. Providwd you ditch the size variable in the for loop. And you need to define broken as a table before the loop begins
Edited by CometWolf, 19 February 2014 - 01:28 PM.

## TheOddByte19 Feb 2014

Yes it would work, But it isn't numbers when they have ( " " ) around them, Then they are strings.
Without them they are numbers and then it wouldn't work.

Here's how you can convert numbers into strings and turn numbers that are strings into numbers again
```local number = 1

--# Converting the number into a string
number = tostring( number ) -- number = "1"
print( type( number ) ) -- prints out string

-# Converting the number into a number
number = tonumber( number ) -- number = 1
print( type( number ) ) -- prints out number
```

Edit:
The current code in the for loop you have would cause strings to become like this
```local text = "Hello"
local t = breakUp( text, #text )
--[[
Output:

H
He
Hel
Hell
Hello
--]]
```

Now to fix this the loop should look like this
```for i = 1, size do
broken[i] = string.sub( text, i, i )
end
```

Edited by Hellkid98, 19 February 2014 - 01:30 PM.

## CometWolf19 Feb 2014

String.sub works on numbers too, lua will just convert it auromatically.

Also, the function is called string.format.
Edited by CometWolf, 19 February 2014 - 01:30 PM.

## MrabEzreb19 Feb 2014

I know, numbers in a string. And the "size" argument is the size of the CHUNKS:
```breakUp("ABCD", 1) = {"A", "B", "C", "D"}
breakUp("ABCD", 2) = {"AB", "CD"}
```
Accually, the "breakUp("ABCD", 2) would give {"AB", "BC", "CD", "D"}, but that could be fixed.

## TheOddByte19 Feb 2014

CometWolf, on 19 February 2014 - 01:29 PM, said:

String.sub works on numbers too, lua will just convert it auromatically.

Also, the function is called string.format.
Oh ok, Thought it would error if you did that.
One question to the OP, Are size really needed? Since I'm wondering if you're just using it to breakup the whole word into letters.
If so then here's how you could do that
```local function breakUp( text )
local letters = {}
for i = 1, #text do
letters[i] = string.sub( text, i, i )
end
return letters
end
```

## MrabEzreb19 Feb 2014

Ik, but I made the function "API-Ready," with customization. Yes, I only need individual letters, but someone else might want groups of two or even three.

MrabEzreb, on 19 February 2014 - 01:32 PM, said:

I know, numbers in a string. And the "size" argument is the size of the CHUNKS:
```breakUp("ABCD", 1) = {"A", "B", "C", "D"}
breakUp("ABCD", 2) = {"AB", "CD"}
```
Accually, the "breakUp("ABCD", 2) would give {"AB", "BC", "CD", "D"}, but that could be fixed.
Accually, it would NOT give {"AB", "BC", "CD", "D"}, it would give {"AB", "CD"}.