KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > util > StringUtil


1 package polyglot.util;
2
3 /** String utilities. */
4 public class StringUtil
5 {
6     /**
7      * Given the name for a class, returns the portion which appears to
8      * constitute the package -- i.e., all characters up to but not including
9      * the last dot, or no characters if the name has no dot.
10      **/

11     public static String JavaDoc getPackageComponent(String JavaDoc fullName) {
12     int lastDot = fullName.lastIndexOf('.');
13     return lastDot >= 0 ? fullName.substring(0,lastDot) : "";
14     }
15    
16     /**
17      * Given the name for a class, returns the portion which appears to
18      * constitute the package -- i.e., all characters after the last
19      * dot, or all the characters if the name has no dot.
20      **/

21     public static String JavaDoc getShortNameComponent(String JavaDoc fullName) {
22     int lastDot = fullName.lastIndexOf('.');
23     return lastDot >= 0 ? fullName.substring(lastDot+1) : fullName;
24     }
25
26     /**
27      * Returns true iff the provided class name does not appear to be
28      * qualified (i.e., it has no dot.)
29      **/

30     public static boolean isNameShort(String JavaDoc name) {
31     return name.indexOf('.') < 0;
32     }
33
34     public static String JavaDoc getFirstComponent(String JavaDoc fullName) {
35     int firstDot = fullName.indexOf('.');
36     return firstDot >= 0 ? fullName.substring(0,firstDot) : fullName;
37     }
38
39     public static String JavaDoc removeFirstComponent(String JavaDoc fullName) {
40     int firstDot = fullName.indexOf('.');
41     return firstDot >= 0 ? fullName.substring(firstDot+1) : "";
42     }
43  
44     public static String JavaDoc escape(String JavaDoc s) {
45         return escape(s, false);
46     }
47
48     public static String JavaDoc escape(char c) {
49         return escape(String.valueOf(c), false);
50     }
51
52     public static String JavaDoc unicodeEscape(String JavaDoc s) {
53         return escape(s, true);
54     }
55
56     public static String JavaDoc unicodeEscape(char c) {
57         return escape(String.valueOf(c), true);
58     }
59
60     public static String JavaDoc escape(String JavaDoc s, boolean unicode) {
61         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s.length());
62
63     for (int i = 0; i < s.length(); i++) {
64         char c = s.charAt(i);
65         escape(sb, c, unicode);
66     }
67
68     return sb.toString();
69     }
70
71     private static void escape(StringBuffer JavaDoc sb, char c, boolean unicode) {
72         if (c > 0xff) {
73             if (unicode) {
74                 String JavaDoc s = Integer.toHexString(c);
75                 while (s.length() < 4) s = "0" + s;
76                 sb.append("\\u" + s);
77             }
78             else {
79                 sb.append(c);
80             }
81         return;
82     }
83
84     switch (c) {
85         case '\b': sb.append("\\b"); return;
86         case '\t': sb.append("\\t"); return;
87         case '\n': sb.append("\\n"); return;
88         case '\f': sb.append("\\f"); return;
89         case '\r': sb.append("\\r"); return;
90         case '\"': sb.append("\\" + c); return; // "\\\"";
91
case '\'': sb.append("\\" + c); return; // "\\\'";
92
case '\\': sb.append("\\" + c); return; // "\\\\";
93
}
94
95         if (c >= 0x20 && c < 0x7f) {
96             sb.append(c);
97         return;
98     }
99
100         sb.append("\\" + (char) ('0' + c / 64)
101                        + (char) ('0' + (c & 63) / 8)
102                        + (char) ('0' + (c & 7)));
103     }
104     
105     public static String JavaDoc nth(int n) {
106         StringBuffer JavaDoc s = new StringBuffer JavaDoc(String.valueOf(n));
107         if (s.length() > 1) {
108             if (s.charAt(s.length()-2) == '1') {
109                 // all the teens end in "th", e.g. "11th"
110
s.append("th");
111                 return s.toString();
112             }
113         }
114         
115         char last = s.charAt(s.length()-1);
116         switch (last) {
117             case '1':
118                 s.append("st");
119                 break;
120             case '2':
121                 s.append("nd");
122                 break;
123             case '3':
124                 s.append("rd");
125                 break;
126             default:
127                 s.append("th");
128                             
129         }
130         return s.toString();
131     }
132 }
133
Popular Tags