Search code examples
lua

Merging 2 strings together?


I have two strings 

String1 = "h l o w r d"
String2 = " e l   o l !"

I want to combine them so I will have a result like this:

hello world!

I don't know how to do that or what methods I have to use.


Solution

  • you can read strings byte by byte, and compare characters like this:

    local String1 = "h l o w r d"
    local String2 = " e l   o l !"
    local s = ""
    local i = 1
    repeat
       local b1, b2 = String1:byte(i), String2:byte(i)
       s = s .. string.char( (b2==nil or b1~=32)  and b1 or b2)
       i = i+1
       print(i,s)
    until not (String1:byte(i) or String2:byte(i) )