개발 이모저모

StringUtils.hasText 함수

진진이랑 2025. 1. 5. 19:09
728x90
반응형


StringUtils.hasText는 Spring Framework에서 제공하는 유틸리티 메서드로, 문자열이 비어 있지 않고 공백이 아닌 문자(텍스트)를 포함하고 있는지를 확인하는 함수이다. (org.springframework.util.StringUtils 클래스에 포함)

public static boolean hasText(@Nullable String str)

반환값은, 문자열이 비어 있지 않고 공백 외의 텍스트를 포함하고 있으면 true, 그렇지 않으면 false를 반환한다.


예시:

1. 문자열에 텍스트가 있는 경우

String str = "Hello World";
System.out.println(StringUtils.hasText(str)); // 출력: true

 

2. 문자열이 공백으로만 이루어진 경우

String str = "   ";
System.out.println(StringUtils.hasText(str)); // 출력: false

 

3. 문자열이 null인 경우

  String str = null;
  System.out.println(StringUtils.hasText(str)); // 출력: false

 

4. 빈 문자열인 경우

String str = "";
System.out.println(StringUtils.hasText(str)); // 출력: false

 

 

 

StringUtils.hasText()는 공백이나 빈 문자열까지 검증해야 할 때 매우 유용하고, Spring 프로젝트에서 문자열 유효성을 검증하는 데 자주 사용된다!!

 

728x90
반응형