1 package cz.cuni.amis.utils; 2 3 public class StringIdifier { 4 5 public static final String allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; 6 7 public static final String number = "0123456789"; 8 9 public static String idify(String str) { 10 return idify(str, "", "_"); 11 } 12 13 public static String idify(String str, String allowedExtraChars) { 14 return idify(str, allowedExtraChars, "_"); 15 } 16 17 public static String idify(String str, String allowedExtraChars, String replaceChar) { 18 if (str == null) return replaceChar; 19 if (str.length() == 0) return replaceChar; 20 StringBuffer result = new StringBuffer(str.length()); 21 22 if (number.contains(str.substring(0,1))) { 23 result.append(replaceChar); 24 } 25 26 for (int i = 0; i < str.length(); ++i) { 27 String c = str.substring(i,i+1); 28 if (allowed.contains(c) || allowedExtraChars.contains(c)) { 29 result.append(c); 30 } else { 31 result.append(replaceChar); 32 } 33 } 34 35 return result.toString(); 36 } 37 38 }