OH3 substringAfter is undefined for the type String

Hi,
I’m trying to migrate my rules to OH3.
While with DateTime Conversion (openHAB 3.x) there’s quite a good article how to overcome the time conversion issues, I struggle to get my rules working similar when it comes to string manipulation.
Unfortunately, it hasn’t been mentioned that String class apparently isn’t the same as before.

At least for me, OH3 complains about:

The method substringAfter(String) is undefined for the type String
The method substringBefore(String) is undefined for the type String
The method startsWithIgnoreCase(String) is undefined for the type String

Am I just missing some import, or do you have any other tips for me?

Regards,
Stefan D.

You’ve changed Java versions as well I expect, you’ll need to find out what methods your java string now supports.

Hi together,

I have the same question / problem.
I was already looking for the correct namespace to import but my research was not successful. The namespace “org.apache.commons.lang3.StringUtils” is not working for StringUtils.

So, is there someone who knows already the correct namespace?

Is it possible to use substringAfter and some other StringUtils functions anymore in OH3?

Thx a lot!

Is something like this of any use?

String example = "/abc/def/ghfj.doc";
System.out.println(example.substring(example.lastIndexOf("/") + 1));

That’s from Stack Overflow.

Thank you for your answer!
Of course you can use built in functions from String class, but the code it is much more “complex”. The StringUtils class was very easy.
Now it seems that you have to build your own scripts/functions or have to write more complex rules as it was with e.g. substringAfter …

Hi
I agree with daveZ. I don’t understand why one would remove such handy functions from an API.

Well, I helped myself with lambda functions, in order to get the OH3 migration done. In future rules I probably will use cleaner code than that.

Just in case anyone wants to know how I did it:

val Functions$Function2 < String, String, String > subStringAfter = [ origString, subStr |
   var pos = origString.indexOf(subStr)
   var retStr = ""
   if (pos>=0) {
      retStr = origString.substring(pos + subStr.length)
   }
   retStr
] 

val Functions$Function2 < String, String, String > subStringBefore = [ origString, subStr |
   var pos = origString.indexOf(subStr)
   var retStr = ""
   if (pos>=0) {
      retStr = origString.substring(0, pos)
   }
   retStr
] 

And then instead of:

val itemPrefix = "I_GN_Bewaesserung" + triggeringItemName.substringAfter("Bewaesserung").substringBefore("_")

you will use:

val itemPrefix = "I_GN_Bewaesserung" + subStringBefore.apply(subStringAfter.apply(triggeringItemName, "Bewaesserung"), "_")
2 Likes

I found this post on github: