Page 1 of 1

Replace a character in a string

Posted: Saturday 23 November 2019 10:57
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

Re: Replace a character in a string

Posted: Saturday 23 November 2019 11:06
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


Re: Replace a character in a string

Posted: Saturday 23 November 2019 11:14
by Number8
Thanks a lot.

Re: Replace a character in a string

Posted: Saturday 23 November 2019 11:51
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?

Re: Replace a character in a string

Posted: Saturday 23 November 2019 12:35
by zicht
you should escape it

Re: Replace a character in a string  [Solved]

Posted: Saturday 23 November 2019 15:02
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

Re: Replace a character in a string

Posted: Saturday 23 November 2019 15:17
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.