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

Help me with 'splitting' strings?

Started by Tjakka5, 03 June 2013 - 03:28 AM
Tjakka5 #1
Posted 03 June 2013 - 05:28 AM
Hey guys,

Im trying to make something, but for that I need to do 2 things with string that I do not know how to do.


--I have this variable.
local string = string1

--And I want to convert that string into multiple strings, that each
--contain 1 symbol of the string.
local stringPart1 = s
local stringPart2 = t
local stringPart3 = r
local stringPart4 = i
local stringPart5 = n
local stringPart6 = g
local stringPart7 = 1

I also want to count how much symbols the string has, is this possible?
theoriginalbit #2
Posted 03 June 2013 - 05:39 AM
Ok start with the simple one… To count the characters in a string you can use 1 of 3 methods detailed below

local str = "some string"
-- method 1; # symbol
print( #str )
-- method 2; string.len
print( string.len(str) )
-- method 3; string.len again, but using OO
print( str:len() )

now for splitting a string, well you cannot easily split a string into one variable for each character, its too hard, you can never know exactly how many variables you will need to hold the string, so instead we use a table to store each character. we can use string.sub to be able to get a specific character, and a numerical for loop to go through the indexes

function getChars(str)
  local chars = {}
  for i = 1, #str do
	chars[i] = str:sub(i)
  end
  return chars
end

EDIT: Oh also you do realise that strings need a " or ' on either side of it right? because you did miss them in the OP, so all those variables would be nil
Edited on 03 June 2013 - 03:41 AM
Tjakka5 #3
Posted 03 June 2013 - 09:03 AM
Now this is interesting…

I wrote this code to print the word letter after letter, for testing purposes aaaand…

local chars = {}
local letters = read()
print( #letters )
function getChars(letters)
  for i = 1, #letters do
	chars[i] = letters:sub(i)
  end
end
function printChars()
  for i = 1, #letters do
	print("" ..chars[i])
  end
end
getChars(letters)
printChars()

That gives this:


>button
Trampoline
10
Trampoline
rampoline
ampoline
mpoline
poline
oline
line
ine
ne
e

What'd I do wrong this time?

EDIT:
And with this I made this weird scrolly thing:
http://pastebin.com/X7W30e90
Spoilerlocal chars = {}

local letters = read()
print( #letters )

function getChars(letters)
for i = 1, #letters do
chars = letters:sub(i)
end
end

function printChars()
for i = 1, #letters do
print("" ..chars)
sleep(0.1)
term.clear()
term.setCursorPos(1, 1)
end
end

getChars(letters)
printChars()
theoriginalbit #4
Posted 03 June 2013 - 09:04 AM
whoops, sorry that was me… change this
chars[i] = str:sub(i)
to this
chars[i] = str:sub(i, i)

we need to give it a start and an end, or it will assume what we give it is a start and the end will be the end of the string, hence that output.
Tjakka5 #5
Posted 03 June 2013 - 09:07 AM
whoops, sorry that was me… change this
chars[i] = str:sub(i)
to this
chars[i] = str:sub(i, i)

we need to give it a start and an end, or it will assume what we give it is a start and the end will be the end of the string, hence that output.

Ah, okay, thanks :)/>