본문 바로가기
Java/POJO

lang3.StringUtils 사용예

by java개발자 2016. 4. 20.
package org.test.apache.lang;

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;	//commons-lang3-3.4.jar

/**
 * 참고 
 * http://kmj1107.tistory.com/entry/Java-StringUtils
 * https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringUtils.html
 */
public class StringUtilsTest {
	public static void main(String... args) {
		StringUtilsTest s = new StringUtilsTest();
		s.proc();
	}
	public void s(Object o){
		System.out.println(o);
	}
	public void proc() {
		/**
		 * StringUtils 을 사용하는 이유
		 * 기본적으로 NullPointException 방지
		 * 
		 */
		
		//str이 java를 포함하고 있으면 true 반환
		s(StringUtils.contains(null, "java"));		//false
		s(StringUtils.contains("j", "java"));		//false
		s(StringUtils.contains("hi java1", "java"));		//true
		
		//str이 null이면 "", 아니면 str 반환
		s(StringUtils.defaultString(null));				//빈공백
		s(StringUtils.defaultString(""));				//빈공백
		s(StringUtils.defaultString("abc"));			//abc
		s(StringUtils.defaultString(null, "kkk"));		//kkk		<--주의(null일때만)
		s(StringUtils.defaultString("", "kkk"));		//빈공백		<--주의
		s(StringUtils.defaultString("abc", "kkk"));		//abc
		
		s(StringUtils.deleteWhitespace(" a b c "));			//abc
		
		String str7 = "abc1";
		String str8 = "ABC1";
		String str9 = "abc1";
		s(str7.equals(str8));							//false
		s(str8.equals(str9));							//false
		s(str7.equals(str9));							//true
		s(str7 == str8);								//false
		s(str8 == str9);								//false
		s(str7 == str9);								//true
		s(StringUtils.equals(str7, str8));				//false
		s(StringUtils.equals(str8, str9));				//false
		s(StringUtils.equals(str7, str9));				//true
		s(StringUtils.equalsIgnoreCase(str7, str8));	//true
		s(StringUtils.equals(null, null));			//true
		
		s(StringUtils.indexOf("", "ABC"));				//-1
		s(StringUtils.indexOf(null, "ABC"));			//-1
		s(StringUtils.lastIndexOf("", "ABC"));			//-1
		s(StringUtils.lastIndexOf(null, "ABC"));		//-1
		
		s(StringUtils.isEmpty(null));			//true
		s(StringUtils.isEmpty(""));				//true
		s(StringUtils.isEmpty("ABC"));			//false
		s(StringUtils.isNotEmpty(null));		//false
		s(StringUtils.isNotEmpty(""));			//false
		s(StringUtils.isNotEmpty("ABC"));		//true
		
		String[] str1 = {"python", "java", "scala", "db"};
		s(StringUtils.join(str1, " | "));		//python | java | scala | db
		s(StringUtils.join(str1, ", "));		//python, java, scala, db
		
		s(StringUtils.lowerCase("ABcd"));		//abcd
		s(StringUtils.upperCase("ABcd"));		//ABCD
		
		s(StringUtils.reverse("ABCDE"));		//EDCBA
		
		s(Arrays.toString(StringUtils.split("abc, def, ghi, jkl", ",")));	//[abc,  def,  ghi,  jkl]
		
		s(StringUtils.strip("  AB C D  "));		//AB C D
		s(StringUtils.trim("  AB C D  "));		//AB C D
		
		//must be more than 3
		s(StringUtils.abbreviate("abcdefg", 4));		//a...
		s(StringUtils.abbreviate("abcdefg", 5));		//ab...
		s(StringUtils.abbreviate("abcdefg", 6));		//abc...
		s(StringUtils.abbreviate("abcdefghijk", 2, 7));	//abcd...	<-버그???
		
		
		//등등등
		
		
		
		
	}
}


'Java > POJO' 카테고리의 다른 글

디자인 패턴  (0) 2017.09.22