티스토리 뷰
반응형
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 유틸리티는 템플릿 내에서 문자열 조작을 직접 처리할 수 있는 강력한 기능 세트를 제공합니다. 이렇게 하면 더 깔끔한 코드가 보장되고 광범위한 백엔드 문자열 처리의 필요성이 줄어듭니다. 보다 복잡한 작업의 경우 이러한 유틸리티를 백엔드 로직과 결합하면 최적의 결과를 얻을 수 있습니다.
반응형
'개발' 카테고리의 다른 글
[Springboot JPA 게시판] 프로젝트 생성 (0) | 2024.12.02 |
---|---|
ERD란 ? (0) | 2024.11.27 |
프레임워크 학습 로드맵: 개발자를 위한 기초부터 고급 기술까지 (0) | 2024.11.25 |
백엔드 개발을 향상시키는 AI 기반 도구: 종합 가이드 (0) | 2024.11.21 |
Eclipse 문제 해결: 문제 및 수정 사항 (0) | 2024.11.19 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- spring
- commit convetion
- css
- 백엔드개발
- 티스토리챌린지
- Springboot jpa
- JavaScript
- postgresql 다운로드
- 개발자
- 프로그래밍
- 웹개발
- tdd개발
- 항해후기
- html
- postgresql brew
- spring.io.start
- 오블완
- Java
- 캐시스탬피드
- google commit convention
- java test 개발
- ChatGPT
- 더현대 크리스마스 현장대기
- Grammarly
- synchronized 단점
- mock사용법
- API
- 캐시란
- mock해야하는대상과아닌것
- 풀스택
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함