Using #strings utility to use multiple substring functions

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using #strings utility to use multiple substring functions

sabarca
I'm trying to generate different substrings from a single long string. I'm trying to manipulate the string with the #strings utility using substringAfter and substringBefore but the sintax is not working.

Having a string like the next in a variable employee.detaildescription
     "John Wiker : Eployee number 3897 // Sales Department."

I want to extract the name, the employee number and the department using the separators between them with code like:

th:text="${#strings.substringBefore(#strings.substringAfter(employee.detaildescription,'Employee number '),' //')}"

This should return 3897 but it fails.

If I do   th:text="${#strings.substringAfter(employee.detaildescription,'Employee number ')}"   I get the correct string "3897 // Sales Department.". And if I do  
 th:text="${#strings.substringBefore(employee.detaildescription,' :')}"   I get the correct string "John Wiker". But if I want to use a substring function as parameter to other substring function I get nothing.

What's the correct sintax to use functions within functions?
Reply | Threaded
Open this post in threaded view
|

Re: Using #strings utility to use multiple substring functions

Metroids
I'm unable to duplicate your error -- so I'm assuming that your problem is in fact spelling.  In your original string you have:

"John Wiker : Eployee number 3897 // Sales Department."

(notice misspelling of Employee)

And

th:text="${#strings.substringBefore(#strings.substringAfter(employee.detaildescription,'Employee number '),' //')}"

(which has the correct spelling).  here is my test, just to be sure (the correct number shows up).

            <th:block th:with="string = ${'John Wiker : Employee number 3897 // Sales Department.'}">
                <div th:text="${string}" />
                <div th:text="${#strings.substringAfter(string,'Employee number ')}" />
                <div th:text="${#strings.substringBefore(string,' :')}" />
                <div th:text="${#strings.substringBefore(#strings.substringAfter(string,'Employee number '),' //')}" />
            </th:block>

Output:

John Wiker : Employee number 3897 // Sales Department.
3897 // Sales Department.
John Wiker
3897