package com.tienway.util; import java.util.ArrayList; public class StringUtil { public static String ContextType1ToType2(String Type1Context) { if (Type1Context== null || Type1Context.length() == 0) { return ""; } char[] c = Type1Context.toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < c.length; i++) { switch (c[i]) { case '>': sb.append(">"); break; case '<': sb.append("<"); break; case ' ': sb.append(" "); break; case '"': sb.append("""); break; case '&': sb.append("&"); break; case '/n': sb.append("<br>"); break; default: try { sb.append(c[i]); } catch (Exception ex) { } } } return sb.toString(); } public static boolean isLetter(char c) { int k = 0x80; return c / k == 0 ? true : false; } public static int length(String s) { char[] c = s.toCharArray(); int len = 0; for (int i = 0; i < c.length; i++) { len++; if (!isLetter(c[i])) { len++; } } return len; } public static String replaceAll(String strin, String regex, String replacement) { StringBuffer sb = new StringBuffer(); int rbegin = 0; int rend = strin.indexOf(regex); int rlen = regex.length(); while (rend >= 0) { sb.append(strin.substring(rbegin, rend)); sb.append(replacement); rbegin = rend + rlen; rend = strin.indexOf(regex, rbegin); } sb.append(strin.substring(rbegin)); strin = sb.toString(); return strin; } public static String[] split(String strin, char c) { return split(strin, c, 0); } public static String[] split(String strin, char c, int it) { ArrayList arraylist = new ArrayList(); char[] chark = strin.toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = it; i < chark.length; i++) { if (chark[i] == c) { arraylist.add(sb.toString()); sb = new StringBuffer(); } else { try { sb.append(chark[i]); } catch (Exception e) { } if (i == chark.length - 1) { arraylist.add(sb.toString()); } } } int k = arraylist.size(); String as[] = new String[k]; return (String[]) arraylist.subList(0, k).toArray(as); } public static String[] split(String strin, String c) { if (strin == null) { return null; } ArrayList arraylist = new ArrayList(); int begin = 0; int end = 0; while ((begin = strin.indexOf(c, end)) != -1) { String s2 = strin.substring(end, begin); { arraylist.add(strin.substring(end, begin)); } end = begin + c.length(); } if (end != strin.length()) { arraylist.add(strin.substring(end)); } int k = arraylist.size(); String as[] = new String[k]; return (String[]) arraylist.subList(0, k).toArray(as); } public static String toBin(char c) { int k = 0x8000; StringBuffer sb = new StringBuffer(16); for (int i = 0; i < 16; k >>>= 1, i++) { sb.append(((c & k) != 0) ? 1 : 0); } return sb.toString(); } }