01: // Copyright (c) 2001 Per M.A. Bothner and Brainfood Inc.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.lists;
05:
06: /** Various static utility methods for general strings (CharSeqs). */
07:
08: public class Strings {
09: /** Change every character to be uppercase. */
10: public static void makeUpperCase(CharSeq str) {
11: for (int i = str.length(); --i >= 0;)
12: str.setCharAt(i, Character.toUpperCase(str.charAt(i)));
13: }
14:
15: /** Change every character to be lowercase. */
16: public static void makeLowerCase(CharSeq str) {
17: for (int i = str.length(); --i >= 0;)
18: str.setCharAt(i, Character.toLowerCase(str.charAt(i)));
19: }
20:
21: /** Capitalize this string.
22: * Change first character of each word to titlecase,
23: * and change the other characters to lowercase. */
24: public static void makeCapitalize(CharSeq str) {
25: char prev = ' ';
26: int len = str.length();
27: for (int i = 0; i < len; i++) {
28: char ch = str.charAt(i);
29: if (!Character.isLetterOrDigit(prev))
30: ch = Character.toTitleCase(ch);
31: else
32: ch = Character.toLowerCase(ch);
33: str.setCharAt(i, ch);
34: prev = ch;
35: }
36: }
37:
38: public static void printQuoted(
39: /* #ifdef use:java.lang.CharSequence */
40: CharSequence str,
41: /* #else */
42: // CharSeq str,
43: /* #endif */
44: java.io.PrintWriter ps, int escapes) {
45: int len = str.length();
46: ps.print('\"');
47: for (int i = 0; i < len; i++) {
48: char ch = str.charAt(i);
49: if ((ch == '\\' || ch == '\"'))
50: ps.print('\\');
51: else if (escapes > 0) {
52: // These escapes are not standard Scheme or CommonLisp.
53: if (ch == '\n') {
54: ps.print("\\n");
55: continue;
56: } else if (ch == '\r') {
57: ps.print("\\r");
58: continue;
59: } else if (ch == '\t') {
60: ps.print("\\t");
61: continue;
62: }
63: }
64: ps.print(ch);
65: }
66: ps.print('\"');
67: }
68:
69: }
|