[JAVA] reviseAmpersand (이미 “&”인 경우를 제외하고, “&” 기호를 “&”로 치환)
ex 1) &abcde& => &abcde&
ex 2) &abcde&&&& => &abcde&&&&
|
public static void main(String[] args) { String strTest1 = “&abcde&”; strTest1 = reviseAmpersand(strTest1); System.out.println(“strTest1 : ” + strTest1);
// 결과 : strTest1 : &abcde& String strTest2 = “&abcde&&&&”; strTest2 = reviseAmpersand(strTest2); System.out.println(“strTest2 : ” + strTest2);
// 결과 strTest2 : &abcde&&&& } /** * 이미 “&”인 경우를 제외하고, “&” 기호를 “&”로 치환한다. * * @param str * @return */ public static String reviseAmpersand(String str) { if (str == null || str.length() == 0) { return “”; } String oneChar = “”; StringBuffer buff = new StringBuffer(); int len = str.length(); for (int i=0; i<len; i++) { oneChar = str.substring(i, i+1); if (oneChar.equals(“&”) && (i+5 > len || !str.substring(i, i+5).equals(“&”))) { buff.append(“&”); continue; } buff.append(oneChar); } return buff.toString(); }
|