Using substringBefore results in unexpected behaviour

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

Using substringBefore results in unexpected behaviour

john_stephens
This post was updated on .
Hello

I am trying to extract a filename from the file location. The url format is

http://localhost/testenv/23_salesfigure.pdf

I need to extract 23_salesfigure.pdf and 23 from the url. I am able to do the first with

 th:href="${file}" th:text="${#strings.arraySplit(file, '/')[#arrays.length(#strings.arraySplit(file, '/')) - 1]}"

For the second requirement I am using  

th:href="${file}" th:text="${#strings.substringBefore({#strings.arraySplit(file, '/')[#arrays.length(#strings.arraySplit(file, '/')) - 1]},'_')}"

But instead of 23, I am getting [23. I am puzzled as neither the filename nor the the path contains '['
Reply | Threaded
Open this post in threaded view
|

Re: Using substringBefore results in unexpected behaviour

Reiju
th:with="array=${#strings.arraySplit('http://localhost/testenv/23_salesfigure.pdf', '/')},
fileName=${array[array.length - 1]},
result=${#strings.substringBefore(fileName, '_')}"

th:text="${result}"
Reply | Threaded
Open this post in threaded view
|

Re: Using substringBefore results in unexpected behaviour

john_stephens
You are unbelievable!! It works perfectly. Many many thanks

If I want to compare 23 with another variable in the loop and display th:text only if they match, how do i do it? For example:-

tr th:each="branch : ${Branch}"
                                                            td th:text="${branch.id}"
                                                            td th:text="${branch.name}"
                                                            td th:if="${files}"
                                                            span th:each="file : ${files}"
             a th:href="${file}" th:with="array=${#strings.arraySplit(file, '/')},
                                                            fileName=${array[array.length - 1]}, result=${#strings.substringBefore(fileName, '_')}" th:text="${result}">
                                                           
If "${branch.id}" matches with "${result}", then th:text="${result}" should get displayed.
Reply | Threaded
Open this post in threaded view
|

Re: Using substringBefore results in unexpected behaviour

Reiju
looks like, u need declare th:with="... result= ..." in outer html tag. Then th:if="${#strings.equals(result, #strings.toString(SOME_VALUE_FROM_OUTER_LOOP))}"

<table>
    <tr th:each="file : ${files}"
        th:with="array = ${#strings.arraySplit(file, '/')},
                 fileName = ${array[array.length - 1]},
                 result = ${#strings.substringBefore(fileName, '_')}">

        <td th:if="${#strings.equals(result, #strings.toString(SOME_VALUE_FROM_OUTER_LOOP))}"
            th:text="${result}">
        </td>
    </tr>
</table>
Reply | Threaded
Open this post in threaded view
|

Re: Using substringBefore results in unexpected behaviour

john_stephens
You are a genius . God bless you