■ StringUtils은 String이 null에 대해서 null인지 여부를 확인하지 않고(안전한) 문자열 제어를 가능하게 해 준다.
참조 : org.apache.commons.lang3.StringUtils
■ IsEmpty/IsBlank – checks if a String contains text(문자열에 텍스트가 있는지 검사한다).
■ Trim/Strip – removes leading and trailing whitespace(선행 및 후행 공백을 제거한다).
■ Equals/Compare – compares two strings null-safe(두 문자열을 null에 안전하게 비교한다).
■ startsWith – check if a String starts with a prefix null-safe(문자열이 null에 안전하게 주어진 prefix로 시작하는지 검사한다).
■ endsWith – check if a String ends with a suffix null-safe(문자열이 null에 안전하게 주어진 prefix로 끝나는지 검사한다 ).
■ IndexOf/LastIndexOf/Contains – null-safe index-of checks(null에 안전하게 인덱스를 체크한다).
■ IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut – index-of any of a set of Strings
■ ContainsOnly/ContainsNone/ContainsAny – does String contains only/none/any of these characters
■ Substring/Left/Right/Mid – null-safe substring extractions
■ SubstringBefore/SubstringAfter/SubstringBetween – substring extraction relative to other strings
■ Split/Join – splits a String into an array of substrings and vice versa
■ Remove/Delete – removes part of a String
■ Replace/Overlay – Searches a String and replaces one String with another
■ Chomp/Chop – removes the last part of a String
■ AppendIfMissing – appends a suffix to the end of the String if not present
■ PrependIfMissing – prepends a prefix to the start of the String if not present
■ LeftPad/RightPad/Center/Repeat – pads a String
■ UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize – changes the case of a String
■ CountMatches – counts the number of occurrences of one String in another
■ IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable – checks the characters in a String
■ DefaultString – protects against a null input String
■ Rotate – rotate (circular shift) a String
■ Reverse/ReverseDelimited – reverses a String
■ Abbreviate – abbreviates a string using ellipsis or another given String
■ Difference – compares Strings and reports on their differences
■ The StringUtils class defines certain words related to String handling(StringUtils 클래스는 문자열 처리와 관련한 특정 단어를 다음과 같이 정의한다).
null – null
empty – a zero-length string (“”) (길이가 0인 문자열)
space – the space character (‘ ‘, char 32) (char 코드가 32인 문자)
whitespace – the characters defined by Character.isWhitespace(char) (Character.isWhitespace(char)가 true인 문자)
trim – the characters <= 32 as in String.trim() (char 코드가 32보다 같거나 적은 문자)
■ StringUtils handles null input Strings quietly. That is to say that a null input will return null. Where a boolean or int is being returned details vary by method. A side effect of the null handling is that a NullPointerException should be considered a bug in StringUtils. Methods in this class give sample code to explain their operation. The symbol * is used to indicate any input including null.
(StringUtils는 null 입력 문자열을 조용히(?) 처리합니다. 즉, null 입력은 null을 반환합니다. boolean 또는 int형이 반환되는 경우 세부 정보는 메서드에 따라 다릅니다. null 처리의 부작용은 NullPointerException이 StringUtils의 버그로 간주되어야한다는 것입니다. 이 클래스의 메소드는 샘플 코드를 제공하여 작동을 설명합니다. * 기호는 null을 포함한 모든 입력을 나타내는 데 사용됩니다.)
■ pom dependency에 commons-lang3 추가
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> <dependency>
■ IsEmpty/IsBlank(공백인지 여부를 검사한다.)
StringUtils.isEmpty("x"); // false StringUtils.isEmpty(""); // true StringUtils.isEmpty(null); // true StringUtils.isBlank("x"); // false StringUtils.isBlank(""); // true StringUtils.isBlank(null); // true
■ equals
StringUtils.equals(null, null); // true StringUtils.equals(null, "abc"); // false StringUtils.equals("abc", null); // false StringUtils.equals("abc", "abc"); // true StringUtils.equals("abc", "ABC"); // false
■ replaceIgnoreCase(*는 모든 문자열을 표시)
StringUtils.replaceIgnoreCase(null, *, *); // null StringUtils.replaceIgnoreCase("", *, *); // "" StringUtils.replaceIgnoreCase("any", null, *); // "any" StringUtils.replaceIgnoreCase("any", *, null); // "any" StringUtils.replaceIgnoreCase("any", "", *); // "any" StringUtils.replaceIgnoreCase("aba", "a", null); // "aba" StringUtils.replaceIgnoreCase("abA", "A", ""); // "b"
■ defaultString(String str) : 인자가 null이면 default(“”)로 변경한다.
StringUtils.defaultString(null); // "" StringUtils.defaultString(""); // "" StringUtils.defaultString("bat"); // "bat"
■ defaultString(String str, String defaultStr) : str이 null이면 defaultStr로 변경한다.
StringUtils.defaultString(null, "NULL") = "NULL" StringUtils.defaultString("", "NULL") = "" StringUtils.defaultString("bat", "NULL") = "bat"
■ defaultString(String str, String defaultStr) : str이 null이면 defaultStr로 변경한다.
StringUtils.defaultString(null, "NULL") = "NULL" StringUtils.defaultString("", "NULL") = "" StringUtils.defaultString("bat", "NULL") = "bat"
■ countMatches(CharSequence str, char ch), countMatches(CharSequence str, CharSequence sub) : 문자열에서 특정한 문자열, 문자의 갯수를 리턴한다.
StringUtils.countMatches(null, *) = 0 StringUtils.countMatches("", *) = 0 StringUtils.countMatches("abba", null) = 0 StringUtils.countMatches("abba", "") = 0 StringUtils.countMatches("abba", "a") = 2 StringUtils.countMatches("abba", "ab") = 1 StringUtils.countMatches("abba", "xxx") = 0 StringUtils.countMatches(null, *) = 0 StringUtils.countMatches("", *) = 0 StringUtils.countMatches("abba", 0) = 0 StringUtils.countMatches("abba", 'a') = 2 StringUtils.countMatches("abba", 'b') = 2 StringUtils.countMatches("abba", 'x') = 0
- Java에서 properties파일 읽기 2019년 4월 3일
- weblogic.jdbc.wrapper.clob_oracle_sql_clob cannot be cast to oracle.sql.clob 2019년 3월 21일
- Java keytool 사용법 2019년 3월 14일
- Tomcat 시작시 ServletContextListener를 통한 mySQL 접속 2018년 5월 8일
- spring mybatis mysql 연결 예제 2018년 5월 4일
- apache commons StringUtils 예제 2018년 5월 3일
- 자바 래퍼 클래스(Java Wrapper Class)? 2018년 2월 13일
- java string 변수 switch, junit test 2018년 2월 12일
- 단정문 assertEquals와 assertSame의 차이점 2017년 4월 12일
- java continue, break 키워드 2017년 3월 25일
- spring service junit test 2016년 5월 25일
- mybatis mapper junit test 2016년 5월 2일
- JUnit Controller Test java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig 2016년 3월 22일
- java eclipse static import 2016년 3월 15일
- junit 단정문 – Assertions 2016년 3월 14일
- junit & spring-test maven dependency 2016년 3월 14일
- Java String, 자바 문자열 2016년 3월 3일