Jump to content




The String API is VERY confusing


10 replies to this topic

#1 MrabEzreb

  • Members
  • 72 posts

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!

#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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


#3 MrabEzreb

  • Members
  • 72 posts

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?

#4 MrabEzreb

  • Members
  • 72 posts

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 by MrabEzreb, 19 February 2014 - 01:06 PM.


#5 MrabEzreb

  • Members
  • 72 posts

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"}


#6 CometWolf

  • Members
  • 1,283 posts

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 by CometWolf, 19 February 2014 - 01:28 PM.


#7 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

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 by Hellkid98, 19 February 2014 - 01:30 PM.


#8 CometWolf

  • Members
  • 1,283 posts

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 by CometWolf, 19 February 2014 - 01:30 PM.


#9 MrabEzreb

  • Members
  • 72 posts

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.

#10 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 19 February 2014 - 01:34 PM

View PostCometWolf, 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


#11 MrabEzreb

  • Members
  • 72 posts

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.

View PostMrabEzreb, 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"}.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users