This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MrabEzreb's profile picture

The String API is VERY confusing

Started by MrabEzreb, 19 February 2014 - 11:45 AM
MrabEzreb #1
Posted 19 February 2014 - 12:45 PM
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!
Kingdaro #2
Posted 19 February 2014 - 12:50 PM
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
MrabEzreb #3
Posted 19 February 2014 - 12:54 PM
OKAY! So…

word = string
start = number
end = number
string.sub(word, start, end)
that is how string.sub() works?
MrabEzreb #4
Posted 19 February 2014 - 01:05 PM
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 on 19 February 2014 - 12:06 PM
MrabEzreb #5
Posted 19 February 2014 - 01:17 PM
Would it also work on numbers?

letters = breakUp("ABCD")
numbers = breakUp("1234")
letters = {"A", "B", "C", "D"}
numbers = {"1", "2", "3", "4"}
CometWolf #6
Posted 19 February 2014 - 01:26 PM
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 on 19 February 2014 - 12:28 PM
TheOddByte #7
Posted 19 February 2014 - 01:27 PM
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 on 19 February 2014 - 12:30 PM
CometWolf #8
Posted 19 February 2014 - 01:29 PM
String.sub works on numbers too, lua will just convert it auromatically.

Also, the function is called string.format.
Edited on 19 February 2014 - 12:30 PM
MrabEzreb #9
Posted 19 February 2014 - 01:32 PM
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.
TheOddByte #10
Posted 19 February 2014 - 01:34 PM
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
MrabEzreb #11
Posted 19 February 2014 - 01:53 PM
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.
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"}.