Page 1 of 1

Equal string lenght

Posted: Tuesday 18 December 2018 21:35
by DomoticaRob
I want that the strings have the same lenght:

Code: Select all

--This is what I want:
--[123     ]
--[1234    ]
--[     456]
--[    3456]

--This is what I get:
--[123 ]
--[1234 ]
--[ 456]
--[ 3456]


stringOud="123"
stringNieuw = stringLengte(stringOud,6,"R")
print(stringNieuw)

stringOud="1234"
stringNieuw = stringLengte(stringOud,6,"R")
print(stringNieuw)

stringOud="456"
stringNieuw = stringLengte(stringOud,6,"L")
print(stringNieuw)

stringOud="3456"
stringNieuw = stringLengte(stringOud,6,"L")
print(stringNieuw)

function stringLengte(stringOud,lengteGewenst,kantOpvullen)
    local stringNieuw=""
    local lengteBasis=string.len(stringOud)
    local lengteRest=lengteGewenst-lengteBasis
    
    if kantOpvullen=="R" then
        stringNieuw=(stringOud..string.rep(string.char(32),lengteRest))
    elseif kantOpvullen=="L" then
        stringNieuw=(string.rep(string.char(32),lengteRest)..stringOud)
    end
    return stringNieuw
end

The problem with character 32 (space) is that in a print or io.write you don't get all the spaces you want.
I want it because I want a file output with horizontal equal distances.

What is the solution?

Re: Equal string lenght

Posted: Tuesday 18 December 2018 21:55
by jvdz
Yes they are all the same length when I run your posted test script, but you probably check it with a proportional font in stead of a fixed font like courier?
Do you want to display it in a webpage or something?

Jos

Re: Equal string lenght

Posted: Tuesday 18 December 2018 23:54
by DomoticaRob
Hey Jos,

I also have this problem in the Domoticz log.

Unfortunately I can't attach pictures anymore to this forum, I get this error code:

ERROR
Sorry, the board attachment quota has been reached.


Otherwise I could show you a screendump.

When I use for example character 46, that is a dot [.] then it's ok. So all the distances are equal. But not with character 32 [space].
The output of my script is a textfile. When I use it in a texteditor with non-proportional font I also don't get equal distances.

Re: Equal string lenght

Posted: Wednesday 19 December 2018 1:44
by waaren
DomoticaRob wrote: Tuesday 18 December 2018 23:54 I also have this problem in the Domoticz log.
/quote]
Tested your code in a dzVents script (output to log) and I see the expected result.

Code: Select all

2018-12-19 01:39:31.622  Status: dzVents: [123   ]
2018-12-19 01:39:31.622  Status: dzVents: [1234  ]
2018-12-19 01:39:31.622  Status: dzVents: [   456]
2018-12-19 01:39:31.622  Status: dzVents: [  3456]

Re: Equal string lenght

Posted: Wednesday 19 December 2018 13:24
by jvdz
DomoticaRob wrote: Tuesday 18 December 2018 23:54 Hey Jos,
I also have this problem in the Domoticz log.

When I use for example character 46, that is a dot [.] then it's ok. So all the distances are equal. But not with character 32 [space].
The output of my script is a textfile. When I use it in a texteditor with non-proportional font I also don't get equal distances.
So you are looking at the Webpage that displays the Domoticz log? Html will not display multiple spaces unless they are   (None breaking spaces)
When you look at the actual domoticz,log file with an editor that uses a none proportional font, you will see they are there. :)

Jos

Re: Equal string lenght

Posted: Wednesday 19 December 2018 20:45
by DomoticaRob
You're damn right.

Problem solved.

Thanks everybody.