View Javadoc
1 /*** 2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html 3 */ 4 package net.sourceforge.pmd.util; 5 6 public class StringUtil { 7 8 public static String replaceString(String d, char oldChar, String newString) { 9 StringBuffer desc = new StringBuffer(); 10 int index = d.indexOf(oldChar); 11 int last = 0; 12 while (index != -1) { 13 desc.append(d.substring(last, index)); 14 desc.append(newString); 15 last = index + 1; 16 index = d.indexOf(oldChar, last); 17 } 18 desc.append(d.substring(last)); 19 return desc.toString(); 20 } 21 22 public static String replaceString(String inputString, String oldString, String newString) { 23 StringBuffer desc = new StringBuffer(); 24 int index = inputString.indexOf(oldString); 25 int last = 0; 26 while (index != -1) { 27 desc.append(inputString.substring(last, index)); 28 desc.append(newString); 29 last = index + oldString.length(); 30 index = inputString.indexOf(oldString, last); 31 } 32 desc.append(inputString.substring(last)); 33 return desc.toString(); 34 } 35 36 /*** 37 * Appends to a StringBuffer the String src where non-ASCII and 38 * XML special chars are escaped. 39 * @param buf The destination XML stream 40 * @param str The String to append to the stream 41 */ 42 public static void appendXmlEscaped(StringBuffer buf, String src) { 43 int l = src.length(); 44 char c; 45 for(int i=0; i<l; i++) { 46 c = src.charAt(i); 47 if (c > '~') {// 126 48 if (c <= 255) 49 buf.append(ENTITIES[c-126]); 50 else 51 buf.append("&u").append(Integer.toHexString(c)).append(';'); 52 } else if (c == '&') 53 buf.append("&"); 54 else if (c == '"') 55 buf.append("""); 56 else if (c == '<') 57 buf.append("<"); 58 else if (c == '>') 59 buf.append(">"); 60 else 61 buf.append(c); 62 } 63 } 64 65 private static final String[] ENTITIES; 66 static { 67 ENTITIES = new String[256-126]; 68 for(int i=126; i<= 255; i++) 69 ENTITIES[i-126] = "&#" + i + ';'; 70 } 71 72 73 }

This page was automatically generated by Maven