Replace a character in a string  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Replace a character in a string

Post by Number8 »

Hello
I'm running Domoticz 4.1214. Using dzVents: I'm trying to replace a character by another one in a string. Can anyone lead me towards the solution?Thank you
Debian buster on NUC and three RPi with buster.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Replace a character in a string

Post by waaren »

Number8 wrote: Saturday 23 November 2019 10:57 I'm trying to replace a character by another one in a string. Can anyone lead me towards the solution ?
Have a look at this site
You will find something like below ( and a lot of other useful information on Lua)

Code: Select all

local a = 'domoticz'
local b = a:gsub('d','D')
print(b) --> Domoticz

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: Replace a character in a string

Post by Number8 »

Thanks a lot.
Debian buster on NUC and three RPi with buster.
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: Replace a character in a string

Post by Number8 »

As a matter of fact I need to replace a dot '.' by a comma ','. So far it does not work. Is the comma considered as a special character that I should declare another way?
Debian buster on NUC and three RPi with buster.
zicht
Posts: 300
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 2023.1+
Location: NL
Contact:

Re: Replace a character in a string

Post by zicht »

you should escape it
Rpi & Win x64. Using : cam's,RFXCom, LaCrosse, RFY, HuE, google, standard Lua, Tasker, Waze traveltime, NLAlert&grip2+,curtains, vacuum, audioreceiver, smart-heating&cooling + many more (= automate all repetitive simple tasks)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Replace a character in a string  [Solved]

Post by waaren »

Number8 wrote: Saturday 23 November 2019 11:51 As a matter of fact I need to replace a dot '.' by a comma ','. So far it does not work. Is the comma considered as a special character that I should declare another way?
You could do yourself a big favor by trying to use a search engine before asking here. The first hit when searching for 'special character Lua'
Shows this site where you can read how to escape these special chars.

Code: Select all

> a = 'domoticz. dzVents'
b = a:gsub('%.',',')
print(b) --> domoticz, dzVents
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: Replace a character in a string

Post by Number8 »

Thanks @waaren. This is what I did at the first place, I was indeed trying to search for escape in the tutorial without success.
Debian buster on NUC and three RPi with buster.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest