/********************************************************************************************************* * 프로그램명 : MString.java 프로그램설명 : 프로젝트와 관련된 정보를 얻을수 있는 class 작성자 : 강원중 작성일 : 2004.01.06 변경일 : 2003.11.30 **********************************************************************************************************/ package kr.co.kihyun.lang; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MString { private static final Logger LOG = LoggerFactory.getLogger(MString.class); public static String omit(String str, int upkeepLength) { return str.length() > upkeepLength ? str.substring(0, upkeepLength) + "..." : str; } /**************************** convert engine ****************************/ public static String convert(String string, String startStr, String endStr, String inStr) { return convert(new StringBuffer(string), startStr, endStr, inStr); } public static String convert(StringBuffer string, String startStr, String endStr, String inStr) { int strIndex = 0; int endIndex = 0; int count = 0; while (true) { if (count > 0) { strIndex = string.indexOf(startStr, strIndex + startStr.length()); } else { strIndex = string.indexOf(startStr, strIndex); } if (strIndex == -1) break; endIndex = string.indexOf(endStr, strIndex); if (endIndex == -1) break; string.replace(strIndex, endIndex + endStr.length(), inStr); count++; } return string.toString(); } /**************************** convert engine ****************************/ public static String convert(String string, String oldStr, String inStr) { return convert(new StringBuffer(string), oldStr, inStr); } public static String convert(StringBuffer string, String oldStr, String inStr) { int strIndex = 0; int count = 0; while (true) { if (count > 0) { strIndex = string.indexOf(oldStr, strIndex + oldStr.length()); } else { strIndex = string.indexOf(oldStr, strIndex); } if (strIndex == -1) break; string.replace(strIndex, strIndex + oldStr.length(), inStr); count++; } return string.toString(); } /**************************** delete ****************************/ public static String delete(String string, String startStr, String endStr) { return delete(new StringBuffer(string), startStr, endStr); } public static String delete(StringBuffer string, String startStr, String endStr) { int strIndex = 0; int endIndex = 0; int count = 0; while (true) { strIndex = string.indexOf(startStr, strIndex); if (strIndex == -1) break; endIndex = string.indexOf(endStr, strIndex); if (endIndex == -1) break; string = string.delete(strIndex, endIndex + endStr.length()); count++; } return string.toString(); } public static String orDelete(String string, String startStr, String endStr) { return orDelete(new StringBuffer(string), startStr, endStr); } public static String orDelete(StringBuffer string, String startStr, String endStr) { int strIndex = 0; int endIndex = 0; int endPos = 0; int count = 0; String[] endS = endStr.split(","); while (true) { endIndex = string.length(); endPos = -1; strIndex = string.indexOf(startStr, strIndex); if (strIndex == -1) break; // System.out.println("strIndex : "+strIndex); for (int i = 0; i < endS.length; i++) { // System.out.println("pos : "+(string.indexOf(endS[i], strIndex))); if ((string.indexOf(endS[i], strIndex)) > -1) { if (endIndex >= (string.indexOf(endS[i], strIndex))) { endIndex = string.indexOf(endS[i], strIndex); endPos = i; // System.out.println("endPos : "+endPos); } } } // endIndex = string.indexOf(endStr, strIndex); // if(endIndex == -1) break; if (endPos == -1 || endIndex == string.length()) break; string = string.delete(strIndex, endIndex + endS[endPos].length()); count++; } return string.toString(); } public static String delete(String string, String delStr) { return delete(new StringBuffer(string), delStr); } public static String delete(StringBuffer string, String delStr) { int strIndex = 0; int count = 0; while (true) { strIndex = string.indexOf(delStr, strIndex); // string table 삭제 if (strIndex == -1) break; string = string.delete(strIndex, delStr.length()); count++; } return string.toString(); } public static boolean isNull(String s) { return s == null || (s != null && (s.trim().equals("") || s.trim().toLowerCase().equals("null"))); } public static String checkNull(String s) { return MString.checkNull(s, ""); } public static String checkNull(String s, String defaultString) { return MString.isNull(s) ? defaultString : s; } // choyongjun tagFilter job public static boolean isWidthORHeightORColsORRowsORSum(String tdOld) { boolean isColsORRowsORSum = false; int colspan = 0; int rowspan = 0; int width = 0; int height = 0; int sum = 0; colspan = tdOld.indexOf("colspan", colspan); rowspan = tdOld.indexOf("rowspan", rowspan); width = tdOld.indexOf("width", width); height = tdOld.indexOf("height", height); sum = tdOld.indexOf("x:fmla=", sum); if (colspan > 0 || rowspan > 0 || width > 0 || height > 0 || sum > 0) { isColsORRowsORSum = true; } return isColsORRowsORSum; } public static String getString(String[] array, String inStr) { String inData = ""; for (int i = 0; i < array.length; i++) { if (i == 0) { inData += array[i]; } else { inData += (inStr + array[i]); } } return inData; } public static String replaceAll(String str, String oldStr, String newStr) throws StringIndexOutOfBoundsException { if (str == null) return ""; StringBuffer strbuf = new StringBuffer(str); int index = strbuf.indexOf(oldStr); int oldLength = oldStr.length(); int newLength = newStr.length(); while (index != -1) { strbuf.replace(index, index + oldLength, newStr); index = strbuf.indexOf(oldStr, index + newLength); } return strbuf.toString(); } // 문자를 바꾸어주는 메쏘드 //v2. 5.static final 필드 변조 가능성 : Update by KWON,HAN //private는 같은 클래스 내에서만 호출이 가능하므로 접근제한이 없는 public으로 하고 final을 사용하지 않는다. // public static final String replaceString(String str, String oldStr, String newStr) public static String replaceString(String str, String oldStr, String newStr) throws StringIndexOutOfBoundsException { LOG.debug("v2. 5.static final 필드 변조 가능성 : MString.replaceString() {}, {} : Test OK ", str, oldStr); LOG.debug("v2. 5.static final 필드 변조 가능성 : MString.replaceString() {}, {} : Test OK ", str, newStr); //========================================================== StringBuffer strbuf = new StringBuffer(str); int index2 = oldStr.length(); int index = str.indexOf(oldStr); for (int i = 0; i < strbuf.length() && index > -1; i++) { strbuf.replace(index, index + index2, newStr); index = str.indexOf(oldStr, index + 1); } return strbuf.toString(); } public static String[] split(String str, String delim) { int strIndex = -1; int endIndex = str.indexOf(delim); int delimLength = delim.length(); int strLength = str.length(); int size = 0; while (true) { if (endIndex == -1) { if (strIndex < strLength) size++; break; } if (strIndex != endIndex && endIndex != 0) size++; strIndex = endIndex + delimLength; endIndex = str.indexOf(delim, strIndex); } strIndex = 0; endIndex = str.indexOf(delim); String[] strList = new String[size]; for (int i = 0; i < size && strIndex != -1; i++) { if (endIndex == -1) { strList[i] = str.substring(strIndex, str.length()); } else { strList[i] = str.substring(strIndex, endIndex); } strIndex = endIndex + delimLength; endIndex = str.indexOf(delim, strIndex); } return strList; } public static String[] token(String str, String delim) { int strIndex = 0; int endIndex = str.indexOf(delim); int delimLength = delim.length(); int strLength = str.length(); int size = 0; while (true) { if (endIndex == -1) { if (strIndex < strLength) size++; break; } if (strIndex != endIndex) size++; strIndex = endIndex + delimLength; endIndex = str.indexOf(delim, strIndex); } strIndex = 0; endIndex = str.indexOf(delim); String[] strList = new String[size]; int i = 0; while (i < size) { if (endIndex == -1) { strList[i] = str.substring(strIndex, str.length()); break; } else { if (strIndex != endIndex) { strList[i] = str.substring(strIndex, endIndex); i++; } } strIndex = endIndex + delimLength; endIndex = str.indexOf(delim, strIndex); } return strList; } public static boolean isAlphabet(char ch) { String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; if (alphabet.indexOf(Character.toString(ch)) > -1) { return true; } return false; } public static int division(String str) { int length = 0; for (int i = 0; i < str.length(); i++) { if (Character.getType(str.charAt(i)) == 5) { length = length + 2; // System.out.print("이건한글이넹 :: "); // System.out.println(str.charAt(i)); } else { length = length + 1; // System.out.println(str.charAt(i)); } } return length; } public static String getEllipsis(String word, int wordLengthLimit) { return getEllipsis(word, wordLengthLimit, "..."); } public static String getEllipsis(String word, int wordLengthLimit, String symbol) { try { if (word.length() > wordLengthLimit) return word.substring(0, wordLengthLimit) + symbol; return word; } catch (Exception e) { return word; } } }