1 3 package jodd.util; 4 5 8 public class CharUtil { 9 10 12 13 16 public static byte[] toByteArray(char[] carr) { 17 if (carr == null) { 18 return null; 19 } 20 byte[] barr = new byte[carr.length]; 21 for (int i = 0; i < carr.length; i++) { 22 barr[i] = (byte) carr[i]; 23 } 24 return barr; 25 } 26 27 30 public static char[] toCharArray(byte[] barr) { 31 if (barr == null) { 32 return null; 33 } 34 char[] carr = new char[barr.length]; 35 for (int i = 0; i < barr.length; i++) { 36 carr[i] = (char) barr[i]; 37 } 38 return carr; 39 } 40 41 42 44 45 51 public static boolean equalsOne(char c, char[] match) { 52 for (int i = 0; i < match.length; i++) { 53 if (c == match[i]) { 54 return true; 55 } 56 } 57 return false; 58 } 59 60 66 public static int findFirstEqual(char[] source, int index, char[] match) { 67 for (int i = index; i < source.length; i++) { 68 if (equalsOne(source[i], match) == true) { 69 return i; 70 } 71 } 72 return -1; 73 } 74 75 81 public static int findFirstEqual(char[] source, int index, char match) { 82 for (int i = index; i < source.length; i++) { 83 if (source[i] == match) { 84 return i; 85 } 86 } 87 return -1; 88 } 89 90 91 97 public static int findFirstDiff(char[] source, int index, char[] match) { 98 for (int i = index; i < source.length; i++) { 99 if (equalsOne(source[i], match) == false) { 100 return i; 101 } 102 } 103 return -1; 104 } 105 106 112 public static int findFirstDiff(char[] source, int index, char match) { 113 for (int i = index; i < source.length; i++) { 114 if (source[i] != match) { 115 return i; 116 } 117 } 118 return -1; 119 } 120 121 122 124 128 public static boolean isLowerAscii(char c) { 129 return (c >= 'a') && (c <= 'z'); 130 } 131 132 136 public static boolean isUpperAscii(char c) { 137 return (c >= 'A') && (c <= 'Z'); 138 } 139 140 143 public static char toUpperAscii(char c) { 144 if ((c >= 'a') && (c <= 'z')) { 145 c -= (char) 0x20; 146 } 147 return c; 148 } 149 150 151 154 public static char toLowerAscii(char c) { 155 if ((c >= 'A') && (c <= 'Z')) { 156 c += (char) 0x20; 157 } 158 return c; 159 } 160 161 } 162 | Popular Tags |