1

How to extract the first letter from the string in Thymeleaf.

<div th:text="${#strings.substring(person.someProperty, 0, 1)}"></div>

Can someone please tell the difference between them mentioned below as I can use both insides of the div. Which one should I use. What is the role of square bracket

th:text="${ps.name}"

[[${ps.name}]]
  • Just to add to the notes [already provided](https://stackoverflow.com/a/66185318/12567365) by @iswailyildiz: The `[[...]]` syntax is called _expression inlining_ and you can read more about how it works [here](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf#inlining). Most of the time you can (and probably should) use `${...}` but _"there are situations in which we could prefer writing expressions directly into our HTML texts"_. – andrewJames Feb 13 '21 at 14:49

1 Answers1

1

1 - You can try like this to extract the first letter.

<div th:text="${#strings.substring(person.someProperty, 1)}"></div>

2 - It is the same in 2 uses. In some cases, you use both for architectural protection your html structure. In some places, I use square brackets to both add extra tags and avoid using th:remove="tag". Example: https://stackoverflow.com/a/29317466/2039546

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29