substringBetween in openhab 3

Hi,

I have some rule that use substringBeteen with openhab2.

Now I’m migratig to openhab 3 and ia get this error:
‘substringBeteen’ is not a member of ‘java.lang.String’

I’ve tried addig this import:

import org.apache.commons.lang.StringUtils

but nothing changed.

What can I do ?

Thanks

You’ve also changed Java version, to 11 I expect. This is a Java method, you’ll need to see what is available now.

I think most of the Apache Commons stuff was removed in OH 3. You will have to use substring.

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html

Are you sure you did not have a typo? Message is about substringBeteen, not about substringBetween…

Just a typing error :sweat_smile:

Hello @banzaay I double-checked my typo “substringBetween” but I’m unfortunately not able to use the function “substringBetween” in my Rules after the upgrade from openab 2.5 to openhab 3.0. What did you configure to use the org.apache.commons.lang.StringUtils package and it’s function substringBetween? Thanks a lot!

Hello,

I have a rule that stat with a custom commad where I pass some information to add in a mail.

On 2.5 I have substringBetween:

val String object = receivedCommand.toString.substringBetween("(",")") 
val String message = receivedCommand.toString.substringBetween("[","]")

Now on 3.0 I have moved to function String.split:

val String[] receivedCommandSplit = receivedCommand.toString.split("#")
val String object = receivedCommandSplit.get(0)
val String message = receivedCommandSplit.get(1)

Bye

SOLVED:
I’m reading periodially my power-meter and want to parse the different measures to different items. As the result is in one String I have to parse the string an read values that are between the ID and in this case “kWh”.

One string from PowerMeter:

16.7.0(0.005*kW)
0.9.1(21:44:51)
0.9.2(21-05-19)
1.8.1(00001821.483*kWh)
1.8.104(00001807.283)
1.8.1
03(00001742.559)
1.8.2(00001321.076*kWh)
1.8.2*04(00001283.051)

I’m interested in the kWh of the current day which are here in the ID’s 1.8.1(00001821.483kWh) and 1.8.2(00001321.076kWh)

to get only the consumed energy 00001821.483 and 00001321.076, I wrote the following lambda Function:

val Functions$Function3<String, String, String, String> getTextBetween = [
   String text, 
   String textFrom, 
   String textTo |

   var result = "";
    // Cut the beginning of the text to not occasionally meet a      
    // 'textTo' value in it:
   result =
     text.substring(
       text.indexOf(textFrom) + textFrom.length(),
       text.length())
    // Cut the excessive ending of the text:
    result =
      result.substring(
        0,
        result.indexOf(textTo))
    return result
   ]

so now I can read the power-meter and apply the lambda-function:

var energyNT = Double::parseDouble(getTextBetween.apply(meter_payload,"1.8.1(", "*kWh)"))
var energyHT = Double::parseDouble(getTextBetween.apply(meter_payload,"1.8.2(", "*kWh)"))

and have then the desired values energyNT= 1821.483 and energyHT=1321.076.

Just for those looking for a replacment of the substringBetween() function.