I think I’m being an idiot here but I can’t work out what is wrong with this bit of rule.
Basically, it take each character of a string in turn (and processes it later).
I get the log error
Rule 'gcal get events rule': String index out of range: -1
The rule (a subsection):
var String GCALstr = "*openHAB"
var Number GCALlen
rule "gcal get events rule"
when
Item TestSwitch6 changed
then
if(GCALstr.substring(0,1) == "*") {
GCALlen = GCALstr.length
logInfo("GCALlen: ", GCALlen.toString())
var i = 0
while ((i=i+1) < GCALlen +1) {
logInfo("i: ", i.toString())
var String GCALchar = GCALstr.substring(i, 1)
logInfo("GCALchar: ", GCALchar.toString())
}
}
end
The start is always at 0, e.g. if GCALlen=14, the positions are 0-13. When you add the “+1”, the positions wil be 0-14 (15 characters). That’s the String index out of range error (there is no 15th position).
Thanks for the reply. It didn’t work - but I assume you wrote it on the fly.
You’re right, I should have used character manipulation but I’m still bemused as to why my rule didn’t work.
And, I don’t like the Xtend documentation.
Taking the hints from @rlkoshak, this works perfectly - but it didn’t work when GCALchar was declared as a char type explicitly. Not sure why.
var String GCALstr = "*openHAB"
var Number GCALlen
var int i = 1
rule "gcal get events rule"
when
Item TestSwitch6 changed
then
GCALlen = GCALstr.length
logInfo("GCALlen: ", GCALlen.toString())
while (i < GCALstr.length())
{
logInfo("i: ", i.toString)
var GCALchar = GCALstr.charAt(i).toString
logInfo("GCALchar: ", GCALchar.toString)
i=i+1
}
end
I like it when things work. I also like to know why they did and also why things don’t work and here I’m none-the-wiser but at least I have a solution.