태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.
반응형

Java String 앞 또는 뒤의 공백만 제거하기.

String에서 trim 메소드를 사용하면 앞과 뒤의 모든 공백을 제거합니다.

앞 또는 뒤의 공백만 제거하는 방법입니다.

JAVA 11 이상에 앞의 공백 제거 메소드 stripLeading, 뒤의 공백 제거 메소드 stripTrailing가 추가되었습니다.

 

앞의 공백 제거하기
String str = "    ABCD      ";
str = str.stripLeading();

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#stripLeading()

 

String (Java SE 11 & JDK 11 )

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum

docs.oracle.com

 

뒤의 공백 제거하기
String str = "    ABCD      ";
str = str.stripTrailing();

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#stripTrailing()

 

String (Java SE 11 & JDK 11 )

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum

docs.oracle.com

 

참고) replaceAll에 정규식으로 뒤의 공백 제거
String str = "    ABCD      ";
str = str.replaceAll("\\s+$", "");
반응형
블로그 이미지

Dokon Jang

Tag ,

댓글을 달아 주세요