KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > StringUtils


1 package persistence.antlr;
2
3 public class StringUtils {
4     /** General-purpose utility function for removing
5      * characters from back of string
6      * @param s The string to process
7      * @param c The character to remove
8      * @return The resulting string
9      */

10     static public String JavaDoc stripBack(String JavaDoc s, char c) {
11         while (s.length() > 0 && s.charAt(s.length() - 1) == c) {
12             s = s.substring(0, s.length() - 1);
13         }
14         return s;
15     }
16
17     /** General-purpose utility function for removing
18      * characters from back of string
19      * @param s The string to process
20      * @param remove A string containing the set of characters to remove
21      * @return The resulting string
22      */

23     static public String JavaDoc stripBack(String JavaDoc s, String JavaDoc remove) {
24         boolean changed;
25         do {
26             changed = false;
27             for (int i = 0; i < remove.length(); i++) {
28                 char c = remove.charAt(i);
29                 while (s.length() > 0 && s.charAt(s.length() - 1) == c) {
30                     changed = true;
31                     s = s.substring(0, s.length() - 1);
32                 }
33             }
34         } while (changed);
35         return s;
36     }
37
38     /** General-purpose utility function for removing
39      * characters from front of string
40      * @param s The string to process
41      * @param c The character to remove
42      * @return The resulting string
43      */

44     static public String JavaDoc stripFront(String JavaDoc s, char c) {
45         while (s.length() > 0 && s.charAt(0) == c) {
46             s = s.substring(1);
47         }
48         return s;
49     }
50
51     /** General-purpose utility function for removing
52      * characters from front of string
53      * @param s The string to process
54      * @param remove A string containing the set of characters to remove
55      * @return The resulting string
56      */

57     static public String JavaDoc stripFront(String JavaDoc s, String JavaDoc remove) {
58         boolean changed;
59         do {
60             changed = false;
61             for (int i = 0; i < remove.length(); i++) {
62                 char c = remove.charAt(i);
63                 while (s.length() > 0 && s.charAt(0) == c) {
64                     changed = true;
65                     s = s.substring(1);
66                 }
67             }
68         } while (changed);
69         return s;
70     }
71
72     /** General-purpose utility function for removing
73      * characters from the front and back of string
74      * @param s The string to process
75      * @param head exact string to strip from head
76      * @param tail exact string to strip from tail
77      * @return The resulting string
78      */

79     public static String JavaDoc stripFrontBack(String JavaDoc src, String JavaDoc head, String JavaDoc tail) {
80         int h = src.indexOf(head);
81         int t = src.lastIndexOf(tail);
82         if (h == -1 || t == -1) return src;
83         return src.substring(h + 1, t);
84     }
85 }
86
Popular Tags