2018 04 29 Java - RYUDONGJIN/Memo_wiki GitHub Wiki

[JAVA] StringUtils ๋ฅผ ์ด์šฉํ•˜๊ณ , ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ 'naver', 'google', 'facebook', 'apple'์„ ๋ชจ๋‘ 'asnet'์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์‹ญ์‹œ์˜ค.

public class test {

	public static void main(String[] args) { //๋ฉ”์ธํ•จ์ˆ˜ ์‹คํ–‰ ๋ถ€๋ถ„
		
		String naver = "naver";
		String google = "google";
		String facebook = "facebook";
		String apple = "apple";
		String result1 = null; //ifReplaceStr์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋‹ด์„ ๋ณ€์ˆ˜ result1
		String result2 = null; //switchReplaceStr์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋‹ด์„ ๋ณ€์ˆ˜ result2
		
		System.out.println("๋ณ€๊ฒฝ ์ „ ๊ฐ’ : " + facebook);
		System.out.println("====== ifReplaceStr ํ•จ์ˆ˜ ์‹คํ–‰ ======");
		result1 = ifReplaceStr(facebook, "asnet"); //facebook์ด ๋“ค์–ด๊ฐ”์„ ๋•Œ asnet์œผ๋กœ ๋ณ€๊ฒฝ ํ›„ ๋ฐ˜ํ™˜
		System.out.println("๋ณ€๊ฒฝ ํ›„ ๊ฐ’: " + result1 + "\n");
		
		System.out.println("๋ณ€๊ฒฝ ์ „ ๊ฐ’ : " + apple);
		System.out.println("====== switchReplaceStr ํ•จ์ˆ˜ ์‹คํ–‰ ======");
		result2 = switchReplaceStr(apple, "asnet"); //apple์ด ๋“ค์–ด๊ฐ”์„ ๋•Œ asnet์œผ๋กœ ๋ณ€๊ฒฝ ํ›„ ๋ฐ˜ํ™˜
		System.out.println("๋ณ€๊ฒฝ ํ›„ ๊ฐ’ : " + result2);
		
	}
	//if์™€ else if๋ฌธ์„ ์‚ฌ์šฉํ•œ ๋ฌธ์ž์—ด ๋ณ€๊ฒฝ ํ•จ์ˆ˜
	private static String ifReplaceStr(String inStr1, String outStr1) { //๋งค๊ฐœ๋ณ€์ˆ˜ inStr1๊ณผ outStr1์„ ๋ฐ›๋Š”๋‹ค
		
		String resultStr1 = null; //๋ฆฌํ„ดํ•  ๋ณ€์ˆ˜ resultStr1๋ฅผ null๋กœ ์ดˆ๊ธฐํ™”
		
		if(inStr1.equals("naver")) { //๋ณ€์ˆ˜ inStr1๊ฐ€ "naver"์™€ ๊ฐ™์œผ๋ฉด
			resultStr1 = inStr1.replace(inStr1, outStr1); //inStr1์„ outStr1์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ resultStr1์— ๋‹ด๋Š”๋‹ค - (A)
		} else if(inStr1.equals("google")) { //๋ณ€์ˆ˜ inStr1๊ฐ€ "google"๊ณผ ๊ฐ™์œผ๋ฉด
			resultStr1 = inStr1.replace(inStr1, outStr1); //(A)
		} else if(inStr1.equals("facebook")) { //๋ณ€์ˆ˜ inStr1๊ฐ€ "facebook"๊ณผ ๊ฐ™์œผ๋ฉด
			resultStr1 = inStr1.replace(inStr1, outStr1); //(A)
		} else if(inStr1.equals("apple")) { //๋ณ€์ˆ˜ inStr1๊ฐ€ "apple"๊ณผ ๊ฐ™์œผ๋ฉด
			resultStr1 = inStr1.replace(inStr1, outStr1); //(A)
		}
		return resultStr1; //resultStr1๊ฐ’์„ ๋ฆฌํ„ด
	}
	
	//switch~case๋ฌธ์„ ์‚ฌ์šฉํ•œ ๋ฌธ์ž์—ด ๋ณ€๊ฒฝ ํ•จ์ˆ˜
	private static String switchReplaceStr(String inStr2, String outStr2) { //๋งค๊ฐœ๋ณ€์ˆ˜ inStr2๊ณผ outStr2์„ ๋ฐ›๋Š”๋‹ค
		
		String resultStr2 = null; //๋ฆฌํ„ดํ•  ๋ณ€์ˆ˜ resultStr2๋ฅผ null๋กœ ์ดˆ๊ธฐํ™”
		
		switch(inStr2) { //๋ณ€์ˆ˜ inStr2๋ฅผ ๋น„๊ตํ•  ๊ฐ’์œผ๋กœ ์„ค์ •
		case "naver" : //"naver"๋ผ๋Š” case
			resultStr2 = inStr2.replace(inStr2, outStr2); //inStr2์„ outStr2์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ resultStr2์— ๋‹ด๋Š”๋‹ค - (B)
			break; //case๊ฐ€ ๋งž๋‹ค๋ฉด (B)์‹คํ–‰ ํ›„ switch๋ฌธ ์ข…๋ฃŒ -(C)
		case "google" : //"google"์ด๋ผ๋Š” case 
			resultStr2 = inStr2.replace(inStr2, outStr2); //(B)
			break; //(C)
		case "facebook" : //"facebook"์ด๋ผ๋Š” case
			resultStr2 = inStr2.replace(inStr2, outStr2); //(B)
			break; //(C)
		case "apple" : //"apple"์ด๋ผ๋Š” case
			resultStr2 = inStr2.replace(inStr2, outStr2); //(B)
			break; //(C)
		}
		return resultStr2;  //resultStr2๊ฐ’์„ ๋ฆฌํ„ด
	}
}