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(CharSeq str, java.io.PrintWriter ps,
39: int escapes) {
40: int len = str.length();
41: ps.print('\"');
42: for (int i = 0; i < len; i++) {
43: char ch = str.charAt(i);
44: if ((ch == '\\' || ch == '\"'))
45: ps.print('\\');
46: else if (escapes > 0) {
47: // These escapes are not standard Scheme or CommonLisp.
48: if (ch == '\n') {
49: ps.print("\\n");
50: continue;
51: } else if (ch == '\r') {
52: ps.print("\\r");
53: continue;
54: } else if (ch == '\t') {
55: ps.print("\\t");
56: continue;
57: }
58: }
59: ps.print(ch);
60: }
61: ps.print('\"');
62: }
63:
64: }
|