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

Splitting strings

Started by DeclipZ, 04 April 2015 - 02:45 PM
DeclipZ #1
Posted 04 April 2015 - 04:45 PM
How to split strings? For example:
Before:
string = "text"
After:
chars = { "t","e","x","t" }
Write example code, please.
Lyqyd #2
Posted 04 April 2015 - 04:46 PM
Well, you'd want to iterate through the string and take a single-character substring at the current position and put it into a table. You'd want to look into string.len, string.sub, and table.insert, as well as basic for loops.
DeclipZ #3
Posted 04 April 2015 - 04:51 PM
Well, you'd want to iterate through the string and take a single-character substring at the current position and put it into a table. You'd want to look into string.len, string.sub, and table.insert, as well as basic for loops.
Ok, thank you, I'll try to make it.

I have some noob code


local len = string.len(string)
for i=1,len do
  tableStr[i] = string.sub(string, i, i)
end
Edited on 04 April 2015 - 02:58 PM
Creator #4
Posted 04 April 2015 - 05:00 PM
Do something like this

local chars = {}
Local text = "hello"
For i= 1, text:len() do
chars[#chars+1] = text:sub(i,i)
End

Ready, sorry for the format, it difficult to write code on the phone.

I hope I helped you. ;)/>
Lyqyd #5
Posted 04 April 2015 - 05:43 PM

local len = string.len(string)
for i=1,len do
  tableStr[i] = string.sub(string, i, i)
end

That'll essentially do it! You'll want to declare the table beforehand (local tableStr = {}), and obviously you can't call the variable your string is in "string", but that should work otherwise.
HDeffo #6
Posted 04 April 2015 - 05:59 PM
just to list other possible methods you can use you might also want to use lua patterns to iterate through the string. This way if you wanted to only include certain characters or exclude others you could. Here would be an example to grab everything and split it into a table


local chars = {}
local str = "text example"
for split in str:gmatch(".") do
   table.insert(chars,split)
end
TheOddByte #7
Posted 04 April 2015 - 09:21 PM
Does this work for you?

local function split( text )
    local t = {}
    for i = 1, #text do
        table.insert( t, text:sub( i, i ) )
    end
    return t
end

--# Example usage, inconvenient way to write "Hello World!" :-P
for _, char in ipairs( split( "Hello World!" ) ) do
    write( char )
end

I'll try to explain everything as best as I can, I think you know some of the basics atleast( for loops, functions, variable types )
So when we first loop through the text we start at 1, and continue until the end of the string, #text gets the length of the string or table, it would essentially be the same as doing string.len( text ), but it's simpler and therefore I'll go with it.

In the loop we use table.insert to insert the character into the table we'll return from the function, we get each character by using string.sub( startIndex, lastIndex )
Now we use the loop variable( "i" in this example ) and sub the string to that, we use the same variable on the startIndex and the lastIndex so we get the character there.

When we've finally looped through the string we simply return it.