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๊ฐ์ ๋ฆฌํด
}
}