View Javadoc

1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.admin.servlet;
18  
19  class StringUtils {
20  
21  	/**
22  	 * 文字列を置き換えます。
23  	 * 
24  	 * @param text
25  	 *            テキスト
26  	 * @param fromText
27  	 *            置き換え対象のテキスト
28  	 * @param toText
29  	 *            置き換えるテキスト
30  	 * @return 結果
31  	 */
32  	public static final String replace(final String text,
33  			final String fromText, final String toText) {
34  
35  		if (text == null || fromText == null || toText == null) {
36  			return null;
37  		}
38  		final StringBuilder builder = new StringBuilder(100);
39  		int pos = 0;
40  		int pos2 = 0;
41  		while (true) {
42  			pos = text.indexOf(fromText, pos2);
43  			if (pos == 0) {
44  				builder.append(toText);
45  				pos2 = fromText.length();
46  			} else if (pos > 0) {
47  				builder.append(text.substring(pos2, pos));
48  				builder.append(toText);
49  				pos2 = pos + fromText.length();
50  			} else {
51  				builder.append(text.substring(pos2));
52  				break;
53  			}
54  		}
55  		return builder.toString();
56  	}
57  
58  }