How to Split String in Thymeleaf with Delimiter

2 Views
Reply
L
Legacy User 1
10/25/2023, 11:54:47 AM

Best way to split a string in thymeleaf with delimiter

1 Reply

L
Legacy User 1
10/25/2023, 11:57:07 AM

In Thymeleaf, you can split a string into multiple parts using the #strings utility object, which provides various string manipulation functions, including splitting a string. Here's how you can split a string using Thymeleaf:

Add the Thymeleaf xmlns:th attribute to your HTML document to enable Thymeleaf expressions.

Use Thymeleaf expressions to split a string. Here's an example:

In the code above:

  • th:with is used to define a variable named inputString with the value 'apple,banana,cherry'
  • th:with is used again to define a variable named stringArray by splitting the inputString using the #strings.arraySplit() function with the ',' delimiter.
  • th:each is used to iterate over the elements in the stringArray, and th:text is used to display each part in an HTML list.

After processing, the HTML will display:

- apple

  • banana
  • cherry

You can replace 'apple,banana,cherry' with your own string and '****,' with your desired delimiter to split the string as needed.

Leave a Reply

All replies are subject to pre-moderation. Your reply will appear publicly once approved.

S
SPYTM

Experience the pinnacle of digital communication. The ultimate ultra-premium platform designed for those who demand excellence.

© 2026 SPYTM. All rights reserved.