01: package persistence.antlr;
02:
03: public class StringUtils {
04: /** General-purpose utility function for removing
05: * characters from back of string
06: * @param s The string to process
07: * @param c The character to remove
08: * @return The resulting string
09: */
10: static public String stripBack(String 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 stripBack(String s, String 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 stripFront(String 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 stripFront(String s, String 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 stripFrontBack(String src, String head,
80: String tail) {
81: int h = src.indexOf(head);
82: int t = src.lastIndexOf(tail);
83: if (h == -1 || t == -1)
84: return src;
85: return src.substring(h + 1, t);
86: }
87: }
|