Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split string in Thymeleaf

I have imageNames like wange/25011.jpg|wange/25011-1.jpg or null, I want to split them to wange/25011.jpg and wange/25011-1.jpg, or no split if null. I tried code as below, but no work...

<td th:if="${brickset.imageNames} != null"  th:each="image : ${#strings.arraySplit(brickset.imageNames, '|')}">
    <a href="#" th:href="${imageBaseUrl + image}">
        <img src="#" th:src="${imageBaseUrl + image}" height="64"/>
    </a>
</td> 
like image 354
mikezang Avatar asked Feb 27 '16 11:02

mikezang


People also ask

How do you use the line break in Thymeleaf?

Thanks Linebreak are correctly displayed BUT when user type escaped character like " or ' it show \" or \' (with backslash).

How do you break a string to the next line?

Split a string at a newline character. When the literal \n represents a newline character, convert it to an actual newline using the compose function. Then use splitlines to split the string at the newline character. Create a string in which two lines of text are separated by \n .

How do you separate a string from the end?

split("-"); We can simply use a character/substring instead of an actual regular expression. Of course, there are certain special characters in regex which we need to keep in mind, and escape them in case we want their literal value. Once the string is split, the result is returned as an array of Strings.

How do you check for empty string in Thymeleaf?

See ${#strings. isEmpty(variable)} . It will check for empty or null and call trim() .


1 Answers

Here is my own answer:

<tbody id="portfolio" class="clear fix">
  <tr th:each="brickset : ${bricksets}" th:alt="${brickset.description}">
    <td>
      <div th:unless="${brickset.imageNames == null}">
        <div th:each="image,status : ${#strings.arraySplit(brickset.imageNames, '|')}">
          <a href="#" th:href="${imageBaseUrl + image}" th:remove="${image} == null ? tag" th:title="${brickset.brand.name}">
            <img src="#" th:src="${imageBaseUrl + image}" height="64" th:remove="${status.index} > 0 ? tag"/>
          </a>
        </div>
      </div>
    </td>
  </tr>
</tbody>
like image 62
mikezang Avatar answered Sep 28 '22 16:09

mikezang