KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > CharUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 /**
6  * Various character utilities.
7  */

8 public class CharUtil {
9
10     // ---------------------------------------------------------------- conversions
11

12
13     /**
14      * Converts char array into byte array. Chars are truncated to byte size.
15      */

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     /**
28      * Converts byte array to char array.
29      */

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     // ---------------------------------------------------------------- find
43

44     
45     /**
46      * Match if one character equals to any of the given character.
47      *
48      * @return <code>true</code> if characters match any chararacter from given array,
49      * otherwise <code>false</code>
50      */

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     /**
61      * Finds index of the first character in given array the matches any from the
62      * given set of characters.
63      *
64      * @return index of matched character or -1
65      */

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     /**
76      * Finds index of the first character in given array the matches any from the
77      * given set of characters.
78      *
79      * @return index of matched character or -1
80      */

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     /**
92      * Finds index of the first character in given array the differes from the
93      * given set of characters.
94      *
95      * @return index of matched character or -1
96      */

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     /**
107      * Finds index of the first character in given array the differes from the
108      * given set of characters.
109      *
110      * @return index of matched character or -1
111      */

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     // ---------------------------------------------------------------- is
123

124     /**
125      * Returns <code>true</code> if specified character is lowercase ascii.
126      * If user uses only ASCIIs, it is much much faster.
127      */

128     public static boolean isLowerAscii(char c) {
129         return (c >= 'a') && (c <= 'z');
130     }
131
132     /**
133      * Returns <code>true</code> if specified character is uppercase ascii.
134      * If user uses only ASCIIs, it is much much faster.
135      */

136     public static boolean isUpperAscii(char c) {
137         return (c >= 'A') && (c <= 'Z');
138     }
139
140     /**
141      * Uppers lowercase ascii char.
142      */

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     /**
152      * Lowers upercase ascii char.
153      */

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