View Javadoc

1   package org.paneris.util;
2   
3   public class StringUtils {
4       
5       private static String allowableChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz0123456789";
6       
7   /**
8   ** this function will repace a one string with another.
9   **/
10  public static String replace(String s,String remove, String replace) {
11      String in = s;
12      String out = "";
13      if (remove.length()>0) {
14          while ((in.indexOf(remove) > -1) && (in.length() > 0)) {
15              int cut1 = in.indexOf(remove);
16              int cut2 = cut1 + remove.length();
17              out += in.substring(0,cut1) + replace;
18              in = in.substring(cut2,in.length());
19          }
20      }
21      out += in;
22      return out;
23  }
24  
25    public static String tr(String s, String from, String to) {
26      StringBuffer sNew = null;
27  
28      for (int i = 0; i < s.length(); ++i) {
29        int t = from.indexOf(s.charAt(i));
30        if (t != -1) {
31          if (sNew == null) sNew = new StringBuffer(s);
32          sNew.setCharAt(i, to.charAt(t));
33        }
34      }
35  
36      return sNew == null ? s : sNew.toString();
37    }
38  
39    public static String remove(String s, char c) {
40      StringBuffer sNew = null;
41  
42      for (int i = 0; i < s.length(); ++i) {
43        char si = s.charAt(i);
44        if (si == c) {
45          if (sNew == null) {
46            sNew = new StringBuffer(s);
47            sNew.setLength(i);
48          }
49        }
50        else
51          if (sNew != null) sNew.append(si);
52      }
53  
54      return sNew == null ? s : sNew.toString();
55    }
56  
57    public static String remove(String s, String cs) {
58      StringBuffer sNew = null;
59  
60      for (int i = 0; i < s.length(); ++i) {
61        char si = s.charAt(i);
62        if (cs.indexOf(si) != -1) {
63          if (sNew == null) {
64            sNew = new StringBuffer(s);
65            sNew.setLength(i);
66          }
67        }
68        else
69          if (sNew != null) sNew.append(si);
70      }
71  
72      return sNew == null ? s : sNew.toString();
73    }
74  
75    public static String capitalised(String s) {
76      if (s == null || s.length() == 0)
77        return s;
78      else {
79        char c = s.charAt(0);
80        char d = Character.toUpperCase(c);
81        return c == d ? s : d + s.substring(1);
82      }
83    }
84    
85    public static String getRandomString(int i) {
86      String result = "";
87      int j = allowableChars.length();
88      for (int a=0;a<i;a++) {
89          int index = new Double(Math.random() * j).intValue() ;
90          result += allowableChars.charAt(index);
91      }
92      return result;
93    }
94  }