/Users/lyon/j4p/src/utils/StringUtils.java

1    package utils; 
2     
3    import java.util.StringTokenizer; 
4     
5    /** 
6     * Copyright DocJava, inc. User: lyon Date: Sep 6, 
7     * 2004 Time: 8:35:04 AM 
8     */ 
9    public class StringUtils { 
10       public static StringBuffer LowerCaseFirstLetter( 
11               String s) { 
12           StringBuffer sb = new StringBuffer(s); 
13           char c = sb.charAt(0); 
14           sb.setCharAt(0,Character.toLowerCase(c)); 
15           return sb; 
16       } 
17    
18       public static String sub(String line, 
19                                String stringToEliminate, 
20                                String replacementString) { 
21           int ssSize = stringToEliminate.length(); 
22           int i = line.indexOf(stringToEliminate); 
23           if (i < 0) return line; 
24           return line.substring(0, i) + 
25                  replacementString 
26                  + line.substring(i + ssSize); 
27       } 
28    
29       public static String replaceFirstInstance( 
30               String inputString, 
31               String searchString, 
32               String replacementString) { 
33           int i = inputString.indexOf(searchString); 
34           if (i == -1) return inputString; 
35           int ssSize = searchString.length(); 
36           String part1 = inputString.substring(0, 
37                                                i); 
38           String part2 = inputString.substring( 
39                   i + ssSize); 
40           return part1 + replacementString + part2; 
41       } 
42    
43       /** 
44        * replaceAll 
45        * 
46        * @param s             input String 
47        * @param searchString  The string to searchFor 
48        * @param replaceString The string to replace 
49        *                      it with 
50        * @return new string 
51        */ 
52       public static String replaceAll(String s, 
53                                       String searchString, 
54                                       String replaceString) { 
55           int i = s.indexOf(searchString); 
56           if (i == -1) return s; 
57           int ssSize = searchString.length(); 
58           while (i != -1) { 
59               String part1 = s.substring(0, i); 
60               String part2 = s.substring( 
61                       i + ssSize); 
62               s = part1 + replaceString + part2; 
63               i = s.indexOf(searchString, 
64                             i + 
65                             replaceString.length()); 
66           } 
67           return s; 
68       } 
69    
70       public static void main(String args[]) { 
71           testBug(); 
72    
73       } 
74    
75       public static String replaceAll(String s, 
76                                       String ss[], 
77                                       String rs) { 
78           for (int i = 0; i < ss.length; i++) 
79               s = replaceAll(s, ss[i], rs); 
80           return s; 
81       } 
82    
83       public static void testBug() { 
84           String s = "c:\\test"; 
85           s = replaceAll(s, "\\", "/"); 
86           System.out.println(s); 
87       } 
88    
89       public static void testReplace() { 
90           String ss[] = {"if", "then", "static"}; 
91           String s = "public static public static if if then static"; 
92           s = replaceAll(s, ss, ""); 
93           System.out.println(s); 
94       } 
95    
96       /** 
97        * Determine how many tokens are in a string 
98        * 
99        * @param l The string to be processed 
100       * @return the number of tokens 
101       */ 
102      public static int addTokens(String l) { 
103          System.out.println(l); 
104          int sum = 0; 
105          StringTokenizer st 
106                  = new StringTokenizer(l, 
107                                        ", \t\r\f\n\"\\;"); 
108          int tc = st.countTokens(); 
109          System.out.println("tc=" + tc); 
110          for (int i = 0; i < tc; i++) { 
111              String s = st.nextToken(); 
112              int j = 0; 
113              try { 
114                  j = Integer.parseInt(s); 
115              } catch (NumberFormatException e) { 
116   
117              } 
118              System.out.println(j); 
119              sum = sum + j; 
120          } 
121          return sum; 
122      } 
123   
124      /** 
125       * Method replaceAll. 
126       * 
127       * @return String 
128       */ 
129      public static String replaceAllSb(String s, String orig, String dest) { 
130          StringBuffer sb = new StringBuffer(); 
131          int pos = 0, lastpos; 
132          do { 
133              lastpos = pos; 
134              pos = s.indexOf(orig, pos); 
135              if (pos == -1) continue; 
136              sb.append(s.substring(lastpos, pos)); 
137              sb.append(dest); 
138              pos += orig.length(); 
139          } while (pos != -1); 
140          sb.append(s.substring(lastpos)); 
141          return sb.toString(); 
142      } 
143  } 
144