KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > Strings


1 // Copyright (c) 2001 Per M.A. Bothner and Brainfood Inc.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.lists;
5
6 /** Various static utility methods for general strings (CharSeqs). */
7
8 public class Strings
9 {
10   /** Change every character to be uppercase. */
11   public static void makeUpperCase(CharSeq str)
12   {
13     for (int i = str.length(); --i >= 0; )
14       str.setCharAt(i, Character.toUpperCase(str.charAt(i)));
15   }
16
17   /** Change every character to be lowercase. */
18   public static void makeLowerCase(CharSeq str)
19   {
20     for (int i = str.length(); --i >= 0; )
21       str.setCharAt(i, Character.toLowerCase(str.charAt(i)));
22   }
23
24   /** Capitalize this string.
25    * Change first character of each word to titlecase,
26    * and change the other characters to lowercase. */

27   public static void makeCapitalize(CharSeq str)
28   {
29     char prev = ' ';
30     int len = str.length();
31     for (int i = 0; i < len; i++)
32       {
33     char ch = str.charAt(i);
34     if (! Character.isLetterOrDigit(prev))
35       ch = Character.toTitleCase(ch);
36         else
37           ch = Character.toLowerCase(ch);
38     str.setCharAt(i, ch);
39     prev = ch;
40       }
41   }
42
43   public static void printQuoted (
44                                   /* #ifdef use:java.lang.CharSequence */
45                                   CharSequence JavaDoc str,
46                                   /* #else */
47                                   // CharSeq str,
48
/* #endif */
49                   java.io.PrintWriter JavaDoc ps, int escapes)
50   {
51     int len = str.length();
52     ps.print ('\"');
53     for (int i = 0; i < len; i++)
54       {
55     char ch = str.charAt(i);
56     if ((ch == '\\' || ch == '\"'))
57       ps.print ('\\');
58     else if (escapes > 0)
59       {
60         // These escapes are not standard Scheme or CommonLisp.
61
if (ch == '\n')
62          { ps.print("\\n"); continue; }
63         else if (ch == '\r')
64           { ps.print("\\r"); continue; }
65         else if (ch == '\t')
66           { ps.print("\\t"); continue; }
67       }
68     ps.print (ch);
69       }
70     ps.print ('\"');
71   }
72
73 }
74
Popular Tags