Can I check the length of a string in Microflow?

5
Can I check the amount of characters of a string in Microflow?
asked
4 answers
5

Yes it's possible but using Java is a lot easier and resource friendly. You just need an input String, an output Integer and the following code:

return StringToCheck.length();

In a microflow you could do the following:

1) Create a variable of type Integer called StringLength with an initial value of 1.
2) Loop through an iterator using the substring function on the string you want to figure out the length of. When the substring isn't equal to the original string, up the StringLength value by 1 and repeat.
3) Keep repeating until the substring of the original string has the same value as the original string and you have found the length of your original string by looking at the current StringLength value.

Eg. String = "Mendix"

StringLength = 1 --> substring('Mendix', 0, StringLength) = 'M'
StringLength = 2 --> substring('Mendix', 0, StringLength) = 'Me'
etc. until finally:
StringLength = 6 --> substring('Mendix', 0, StringLength) = 'Mendix'

answered
5

The above answer is nearly right. But if you make a Java action with a string as input and the length as output, you have to give it the return type Integer/Long, which Java understands as Long, so you have to cast it to long. The correct line in the Java action is: return (long) StringToCheck.length();

There's no option to declare the result of a Java action as "Integer". I find it strange that a Java-based language as Mendix, puts Integer and Long together in one class Long, labeled Integer/Long. Why not use separate datatypes?

answered
0

No, you currently need Java for that.

answered
0

Yes you can do, using length function:

length(stringVariable)

answered