String manipulation, character replacement

I’m trying to see if a string has “keybind:” in it, if it does, replace the single character after it with “Button A” then remove the “keybind:” after.

local function _fixToolTip(_tool:Tool)
	local _splitToolTip = "press keybind:g to do something"

	_splitToolTip = string.gsub(_splitToolTip, "(keybind:)[^|]*()",  "%1" .. "Button A")	
	return _splitToolTip
end

It does remove the “g” but doesn’t remove “keybind:” and the text after it also gets removed. “press keybind:Button A”

I was able to remove the “keybind:” by using another string.gsub which looked for “keybind:” and set it to “”. I’m trying to get it to do it within one gsub, but I still haven’t got there yet.

1 Like
_splitToolTip = string.gsub(_splitToolTip, "(press) ([keybind:])[^|]*()",  "%1" .. " Button A")

A little bit weird, but it gets the job done!

It got rid of the rest of the text though but I managed to fix it.

local function _fixToolTip(_tool:Tool)
	
	local function replace_char(str: string)
		return str:match(":(.*)"):sub(2)
	end
	
	local _splitToolTip = "press keybind:g to do something"

	local _fixedToolTip = replace_char(_splitToolTip:find("keybind:"), _splitToolTip)

	_splitToolTip = string.gsub(_splitToolTip, "(press) ([keybind:])[^|]*()",  "%1" .. " Button A")

	return _splitToolTip.._fixedToolTip
end

print(_fixToolTip())

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.