Jump to content




Help me with 'splitting' strings?


  • You cannot reply to this topic
4 replies to this topic

#1 Tjakka5

  • Members
  • 256 posts

Posted 03 June 2013 - 04: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?

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 June 2013 - 04: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 by theoriginalbit, 03 June 2013 - 04:41 AM.


#3 Tjakka5

  • Members
  • 256 posts

Posted 03 June 2013 - 08: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
Spoiler


#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 June 2013 - 08: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.

#5 Tjakka5

  • Members
  • 256 posts

Posted 03 June 2013 - 08:07 AM

View Posttheoriginalbit, on 03 June 2013 - 08:04 AM, said:

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 :)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users