DK1990
DK1990

Reputation: 478

Thymeleaf for each string replace a substring

I have a list of objects. Every Object has a name with a specific prefix. Now I want to replace this prefix with an empty String.

My Code:

Controller:

@Secured("ROLE_ADMIN")
@RequestMapping(value = "/admin/userManagement", method = RequestMethod.GET)
public String showUserManagement(Model model) {
    model.addAttribute("userList", userDelegate.getAllUserForClient(getCompanyAuthority().getName()));
    model.addAttribute("companyName", getCompanyAuthority().getName());
    return "admin/user_management";
}

My html snippet:

<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>ID</th>
            <th>E-mail</th>
            <th th:text="#{adminArea.userManagementReport}">Reports</th>
            <th th:text="#{adminArea.userManagementRole}"></th>
            <th>Edit</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="user,itrStat : ${userList}">
            <td th:text="${itrStat.index}"></td>
            <td th:text="${user.username}"></td>
            <td>
            <small th:each="report : ${user.reports}"
                th:utext="${#strings.replace(report.name,companyName +                                      '_','')} : ${companyName}"></small>
            <br th:each="report : ${user.reports}"></br></td>
        </tr>
    </tbody>
</table>

the Important part is:

<small th:each="report : ${user.rep orts}"
                    th:utext="${#strings.replace(report.name,companyName + '_','')} : ${companyName}"></small>

For every report name, I want to remove the companyName prefix. So TEST_filename.txt -> filename.txt

Can someone help me?

Upvotes: 3

Views: 13924

Answers (2)

riddle_me_this
riddle_me_this

Reputation: 9145

Use substringAfter:

<small th:each="report : ${user.reports}" th:utext="${#strings.substringAfter(report.name,'_'}></small>

Of course, you'll want to make sure that your company names don't also contain the underscore delimiter before doing this.

Upvotes: 4

k.r.vignesh
k.r.vignesh

Reputation: 1

don’t do that ,try this instead , to split the String based on "_" and take the second word by mentioning the index .

Upvotes: 0

Related Questions