Is it possible to remove letters from strings?

Hiya! I was wondering if it was possible to remove letters from strings? Like if you press backspace is removes a letter? The specific letter i’d like to remove is the letter the player has just pressed on the keyboard I added to the game (Not the players keyboard)

Many thanks :grinning:

18 Likes

try string.gsub, for example, this would remove the dashes in the string

local str = string.gsub("hello-world-123", "-", "")
13 Likes

Yes but the thing is, I am looking for it to remove the number the player has just pressed.

local CUR = 0
local CURR = nil
local numberT = {
	["1"] = nil;
	["2"] = nil;
	["3"] = nil;
	["4"] = nil;
	["5"]= nil;
	["6"] = nil;
	["7"]= nil;
	["8"] = nil;
	["9"] = nil;
	["10"] = nil;
}

for _, v in ipairs(script.Parent:GetChildren()) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			print(v.Name)
			CUR+=1
			numberT[CUR] = v.Name
			CURR = v.Name
			local finalString = ''
			
			for index, value in pairs(numberT) do
				finalString ..= tostring(value)
			end
			print(finalString)
			script.Parent.Parent.DISPLAY.Text = finalString
		end)
	
	end
end
9 Likes
local function removeLastLetter(x: string): string
    return string.sub(x, 1, #x - 1) -- string.sub(string, startPos, endPos) Including every character in the string except for the last one
end

Edit: Misuse of string.sub(), my bad

9 Likes

Doesnt seem to work I apologise.

7 Likes

Are you using it like this?

local function removeLastLetter(x: string): string
    return string.sub(x, 1, #x - 1) -- string.sub(string, startPos, endPos) Including every character in the string except for the last one
end

-- Player presses backspace or whatever

local textLabel = script.Parent.Parent.DISPLAY
local text = removeLastLetter(textLabel.Text)
textLabel.Text = text
8 Likes

No there is a button on the screen that works as the backspace.

8 Likes

I meant logic-wise.
In other words,

you did something similar to this?

7 Likes

No, I did this

local function removeLastLetter(x: string): string
	return string.sub(x, 1, #x - 1)
end

for _, v in ipairs(script.Parent:GetChildren()) do
	local finalString = ''
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			print(v.Name)
			CUR+=1
			numberT[CUR] = v.Name
			CURR = v.Name
			for index, value in pairs(numberT) do
				finalString ..= tostring(value)
			end

			script.Parent.Parent.DISPLAY.Text = finalString
		end)
		elseif v.Name == script.Parent.BACK.Name then
		removeLastLetter(finalString)
	end
end

Which also messed up the original script as it tripled the numbers every time i pressed one.

6 Likes
local function removeLastLetter(x: string): string
	return string.sub(x, 1, #x - 1)
end

for _, v in ipairs(script.Parent:GetChildren()) do
	local finalString = ''
	if v:IsA("TextButton") then
		if v.Name == "BACK" then
			v.MouseButton1Click:Connect(function()
				print(v.Name, " is the back button")
				
				if #finalString > 0 then -- Making sure the display length is bigger than zero
					if #finalString > 1 then
						finalString = removeLastLetter(finalString)
					else
						finalString = ""
					end
				end
				
				script.Parent.Parent.DISPLAY.Text = finalString
			end)
		else
			v.MouseButton1Click:Connect(function()
				print(v.Name)
				CUR+=1
				numberT[CUR] = v.Name
				CURR = v.Name
				for index, value in pairs(numberT) do
					finalString ..= tostring(value)
				end

				script.Parent.Parent.DISPLAY.Text = finalString
			end)
		end
	end
end

The numbers tripling would be a problem with your normal-number-button logic.

7 Likes
	v1=script.Parent.Parent.TextBox  --the text box
	t1="my text-"	--default text
	v1.Text=t1 --set the default text to text box

script.Parent.MouseButton1Click:Connect(function()
	local p1,l1,t2,l3,s1 --bunch of variables
	
	l1=string.len(t1) --l1 is the length of your default text
	l3=string.len(v1.Text) --l3 is the length of you text box's text right now at this stage
	
	if l3~=l1 then -- if the text box text is not equal to the  default text length
		
		s1=string.sub(v1.Text,l1+1,l3-1) 
              --[[use string sub find the 
                  first argument the text box text,
                  second the first letter to search from 
                      l1 is you default text + 1 means anything that was added later
                      l3 is the current textbox text length -1 means remove the last letter

              --]]
		v1.Text=t1..s1--set the text to t1 your default text concatenates with s1 the modified text
	end
end)

tell me if this worked?

5 Likes

I tried that but all that did was make it double. It also didnt remove

6 Likes

No sorry. I forgot to answer mb

4 Likes

any errors?

Summary

(char limit)

5 Likes

No I just couldnt read it well. and I wasn’t sure what to do with it

3 Likes

you have a back space bar? (the gui TextBox)
create a local script there and place this script inside
change v1=to the text box that the text is displayed
and change the default text to anything that you want

4 Likes

Its a text label. And the backspace button is a UI element.
image

3 Likes

ui image button?
and
UI text label

instead of

v1=script.Parent.Parent.TextBox  --the text box

change it to the location of you text label

instead of

script.Parent.MouseButton1Click:Connect(function()

change the script. parent to the image button location

4 Likes

Yes. There is also a keypad ill show the full one. Sorry for the poor ui its temporal.
image

5 Likes

can you show he hierarchy if possible?

2 Likes