티스토리 뷰

개발

Thymeleaf String Functions : #strings

ThisisEmma 2024. 11. 26. 17:25
반응형

Thymeleaf는 Java 기반 애플리케이션, 특히 Spring MVC를 위한 최신 서버측 Java 템플릿 엔진입니다. 템플릿 내에서 문자열을 효율적으로 처리하기 위한 다양한 유틸리티와 기능을 제공합니다. 다음은 Thymeleaf의 문자열 함수 문법과 구문을 요약한 것입니다:


Thymeleaf String Functions

Thymeleaf는 주로 #strings 유틸리티를 통해 액세스할 수 있는 문자열 조작을 위한 내장 유틸리티 개체와 함수를 제공합니다.

1. Common String Functions

  • contains(String, Substring)
    • 문자열에 특정 하위 문자열이 포함되어 있는지 확인합니다.
    • Example: 
    • <p th:text="${#strings.contains('Hello World', 'World')}"></p> <!-- true -->
  • containsIgnoreCase(String, Substring)
    • 'contains'의 대소문자를 구분하지 않는 버전입니다.
    • Example:
      <p th:text="${#strings.containsIgnoreCase('Hello World', 'world')}"></p> <!-- true -->
  • substring(String, startIndex, endIndex)
    • 문자열의 일부를 추출합니다.
    • Example:
      <p th:text="${#strings.substring('Hello World', 0, 5)}"></p> <!-- Hello -->
  • startsWith(String, Prefix)
    • 문자열이 지정된 접두사로 시작하는지 확인합니다.
    • Example:
      <p th:text="${#strings.startsWith('Hello World', 'Hello')}"></p> <!-- true -->
  • endsWith(String, Suffix)
    • 문자열이 지정된 접미사로 끝나는지 확인합니다.
    • Example:
      <p th:text="${#strings.endsWith('Hello World', 'World')}"></p> <!-- true -->
  • equals(String1, String2)
    • 두 문자열이 같은지 확인합니다.
    • Example:
      <p th:text="${#strings.equals('text', 'Text')}"></p> <!-- false -->
  • equalsIgnoreCase(String1, String2)
    • 대소문자를 구분하지 않는 동일성 검사.
    • Example:
      <p th:text="${#strings.equalsIgnoreCase('text', 'Text')}"></p> <!-- true -->

2. Trimming and Padding

  • trim(String)
    • 문자열의 양쪽 끝에서 공백을 제거합니다.
    • Example:
      <p th:text="${#strings.trim(' text ')}"></p> <!-- text -->
  • padStart(String, Length, Char)
    • 원하는 길이에 도달할 때까지 문자열의 시작 부분을 특정 문자로 채웁니다.
    • Example:
      <p th:text="${#strings.padStart('123', 5, '0')}"></p> <!-- 00123 -->
  • padEnd(String, Length, Char)
    • 문자열의 끝 부분을 채웁니다.
    • Example:
      <p th:text="${#strings.padEnd('123', 5, '0')}"></p> <!-- 12300 -->

3. Case Conversion

  • toUpperCase(String)
    • 모든 문자를 대문자로 변환합니다.
    • Example:
      <p th:text="${#strings.toUpperCase('hello')}"></p> <!-- HELLO -->
  • toLowerCase(String)
    • 모든 문자를 소문자로 변환합니다.
    • Example:
      <p th:text="${#strings.toLowerCase('HELLO')}"></p> <!-- hello -->

4. String Splitting and Joining

  • split(String, Delimiter)
    • 구분 기호를 기준으로 문자열을 하위 문자열 배열로 분할합니다.
    • Example:
      <p th:each="word : ${#strings.split('one,two,three', ',')}" th:text="${word}"></p>
      <!-- Outputs: one, two, three -->
  • join(List, Separator)
    • 구분 기호를 사용하여 문자열 목록을 단일 문자열로 결합합니다.
    • Example:
      <p th:text="${#strings.join(Arrays.asList('one', 'two', 'three'), ',')}"></p> <!-- one,two,three -->

5. Empty or Null Handling

  • isEmpty(String)
    • 문자열이 비어 있는지 확인합니다.
    • Example:
      <p th:text="${#strings.isEmpty('')}"></p> <!-- true -->
  • isNotEmpty(String)
    • 문자열이 비어 있지 않은지 확인합니다.
    • Example:
       
      <p th:text="${#strings.isNotEmpty('text')}"></p> <!-- true -->
  • isNullOrEmpty(String)
    • 문자열이 null이거나 비어 있는지 확인합니다.
    • Example:
       
      <p th:text="${#strings.isNullOrEmpty(null)}"></p> <!-- true -->

6. Regular Expressions

  • matches(String, Pattern)
    • 문자열이 주어진 정규식과 일치하는지 확인합니다.
    • Example:
       
      <p th:text="${#strings.matches('12345', '\\d+')}"></p> <!-- true -->

Conclusion

Thymeleaf의 #strings 유틸리티는 템플릿 내에서 문자열 조작을 직접 처리할 수 있는 강력한 기능 세트를 제공합니다. 이렇게 하면 더 깔끔한 코드가 보장되고 광범위한 백엔드 문자열 처리의 필요성이 줄어듭니다. 보다 복잡한 작업의 경우 이러한 유틸리티를 백엔드 로직과 결합하면 최적의 결과를 얻을 수 있습니다.

반응형