How to make a hybrid string

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

make a hybrid string

  1. What is the issue? Include screenshots / videos if possible!

I do not know how to do it

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Search In DevForum

Example:

local str1 = "IEQHG"
local str2 = "ABC"

local str3 = str1:hybrid(str2) -- this not work

print(str3) -- not real result: IEABC

what exactly is a hybrid string???

is to merge two strings but without concate

Do you mean something like this?

function hybrid(string1,string2)
	if string1 and string2 then
		local SplitCharacters = {}
		
		string2 = string2 .. " "
		
		for i = 1,#string1 do
			table.insert(SplitCharacters,#SplitCharacters+1,string.sub(string1,i,i)) -- Insert every single character of the string into a table
		end

		local StartReplacing = #string1-#string2
		StartReplacing += 1
		
		for i, SCh in pairs(SplitCharacters) do
			if i > StartReplacing then
				SplitCharacters[i] = string.sub(string2,i-StartReplacing,i-StartReplacing) -- Replace the characters
			end
		end

		local FinalString = ""

		for _, Ch in pairs(SplitCharacters) do
			FinalString = FinalString .. Ch
		end

		return FinalString
	end
end

print(hybrid("Good morning!","afternoon"))

I may be wrong, just tell me if this is what you want :slight_smile:

2 Likes

This should return Gooafternoon

I’m going to test it to see if it works

it doesn’t work at total but I’m satisfied

What do you want to work as well? Tell me, maybe I can add it.

I found a faster and more efficient way

function hybrid(string1,string2)
assert(typeof(string1) == "string","string expected, got " .. typeof(string1))
assert(typeof(string2) == "string","string expected, got " .. typeof(string2))
return string.sub(string1,1,#string2/2) .. string2
end

I found a faster and more efficient way

function hybrid(string1,string2)
assert(typeof(string1) == "string","string expected, got " .. typeof(string1))
assert(typeof(string2) == "string","string expected, got " .. typeof(string2))
return string.sub(string1,1,#string2/2) .. string2
end

Yeah, that can be used too, but I think my solution is more understandable. It depends.

I think my solution is better.

my solution uses 3 lines of code you use too many

my IQ = 9999999999999999999999999

Shorter? Sure. Better? Well. This post Be careful when using assert() and Why explains that it is better not to use assert() because of bad script performance. Mine script may be 50 lines longer but 50x less script memory consuming.

well at least it doesn’t use 50 asserts

function hybrid(string1, string2)
    if typeof(string1) ~= "string" or typeof(string2) ~= "string" then
        return warn("string expected")
    end
    return string.sub(string1, 1, #string2 / 2)..string2
end
1 Like

my idea

   function hybrid(string1, string2)
        if typeof(string1) ~= "string" then
            error("string expected, got " .. typeof(string1))
    elseif typeof(string2) ~= "string" then
           error("string expected, got " .. typeof(string2))
        end
        return string.sub(string1, 1, #string2 / 2)..string2
    end