# \[SOLVED\] TRIM works correctly on simulator but not on device

**URL:** <https://forums.solar2d.com/t/solved-trim-works-correctly-on-simulator-but-not-on-device/357116>\
**Category:** General Questions/Discussion\
**Created:** [January 12, 2025, 10:28am UTC](https://forums.solar2d.com/t/solved-trim-works-correctly-on-simulator-but-not-on-device/357116 "2025-01-12T10:28:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![waltsir](https://forums.solar2d.com/user_avatar/forums.solar2d.com/waltsir/32/4268_2.png) [@waltsir](https://forums.solar2d.com/u/waltsir)\
**Post date:** [January 12, 2025, 10:28am UTC](https://forums.solar2d.com/t/solved-trim-works-correctly-on-simulator-but-not-on-device/357116/1 "2025-01-12T10:28:57Z")

</div>

Hello,  
If I input spaces only then click OK button, the text is trimed and “OK” is not displayed on the simulator, but on Android yes. Is there a workaroud?

```auto
local function trim(str)
	return string.gsub(str,"^%s*(.-)%s*$","%1")
end

local function touchOK(evt)
	if evt.phase=="ended" then
		fld.text=trim(fld.text)
		
		if fld.text~="" then
			display.newText("OK!",400,750,"Verdana",80)
		end
	end
	return true
end

fld=native.newTextField(400,150,770,100)

rec=display.newRect(400,600,100,100)
rec:addEventListener("touch",touchOK)

```

---

<div class="post-metadata">

**Author:** ![ishtiak](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/3ec8ea/32.png) [@ishtiak](https://forums.solar2d.com/u/ishtiak)\
**Post date:** [January 12, 2025, 4:16pm UTC](https://forums.solar2d.com/t/solved-trim-works-correctly-on-simulator-but-not-on-device/357116/2 "2025-01-12T16:16:12Z")

</div>

I use something like this:

```auto
-- Trim of spaces before/after/multiple together in middle.
local function trim(name)
    local result
    result = string.gsub(name, "%s+", " ")
    result = string.gsub(result, "^%s*", "")
    result = string.gsub(result, "%s*$", "")
    return result
end

```

I am not sure exactly what’s going on, but you can try this code if it works.

---

<div class="post-metadata">

**Author:** ![waltsir](https://forums.solar2d.com/user_avatar/forums.solar2d.com/waltsir/32/4268_2.png) [@waltsir](https://forums.solar2d.com/u/waltsir)\
**Post date:** [January 12, 2025, 4:31pm UTC](https://forums.solar2d.com/t/solved-trim-works-correctly-on-simulator-but-not-on-device/357116/3 "2025-01-12T16:31:08Z")

</div>

I tried others, and yours just now, it’s the same behaviour.  
I don’t understand why, that’s not logic. If the string is empty, why the text is displayed??

it seems the problem comes from the pressed button, because if I write this only:

```auto
fld.text=" "
fld.text=trim(fld.text)

if fld.text~="" then display.newText("OK!",400,750,"Verdana",80) end

```

That’s work, nothing is displayed 🤔

EDIT: I had an idea. I think there’s a delay between the input and it’s real value to update the string, so I did this and it works now 😀

```auto
local function touchOK(evt)
	if evt.phase=="ended" then
		fld.text=trim(fld.text)
		
		timer.performWithDelay(50,function()
			if fld.text~="" then
				display.newText("OK!",400,750,"Verdana",80)
			end
		end,1)
	end
	return true
end

```

---

<div class="post-metadata">

**Author:** ![waltsir](https://forums.solar2d.com/user_avatar/forums.solar2d.com/waltsir/32/4268_2.png) [@waltsir](https://forums.solar2d.com/u/waltsir)\
**Post date:** [January 12, 2025, 5:27pm UTC](https://forums.solar2d.com/t/solved-trim-works-correctly-on-simulator-but-not-on-device/357116/4 "2025-01-12T17:27:48Z")

</div>

I coded a better workaround 😀

````auto
local function touchOK(evt)
	if evt.phase=="began" then
		fld.text=trim(fld.text)
	elseif evt.phase=="ended" then
		if fld.text~="" then
			display.newText("OK!",400,750,"Verdana",80)
		end
	end
	return true
end```
````
