View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.utils;
17  
18  /**
19   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
20   * @version $Id: StringUtils.java,v 1.1 2005/12/18 12:58:15 mvdb Exp $
21   */
22  public class StringUtils {
23    
24    /**
25     * Replace the specified string with the specified char
26     *
27     * @param value the value to do the magic on
28     * @param replace the value to replace
29     * @param c the char to replace it with
30     * @return a new string with replacements
31     */
32    public static String replace(String value, String replace,  char c) {
33      if (value.indexOf(replace) == -1) {
34        return value;
35      }
36      StringBuffer sb = new StringBuffer();
37      for (int i = 0; i < value.length(); i++) {
38        boolean replaceIt = false;
39        for (int j = 0; j < replace.length(); j++) {
40          if (value.charAt(i+j) == replace.charAt(j)) {
41            replaceIt = true;
42          } else {
43            replaceIt = false;
44            break;
45          }
46        }
47        if (replaceIt) {
48          sb.append(c);
49          i+=replace.length()-1;
50        } else {
51          sb.append(value.charAt(i));
52        }
53      }
54      return sb.toString();
55    }
56  
57    /**
58     * Capitalizes a string, which means the first character
59     * will be uppercase and the other characters will be lowercase.
60     * @param constraint
61     */
62    public static String capitalize(String constraint) {
63      if (constraint == null || constraint.length() == 0) {
64        return constraint;
65      }
66      constraint = constraint.toLowerCase();
67      StringBuffer buffer = new StringBuffer();
68      buffer.append(Character.toUpperCase(constraint.charAt(0)));
69      buffer.append(constraint.substring(1));
70      return buffer.toString();
71    }
72  
73  }