apache commons StringUtils 예제

■ 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