View Javadoc

1   /*
2    * Copyright 2004-2009 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  package org.seasar.cubby.internal.util;
17  
18  /**
19   * 文字列操作を行うためのユーティリティクラスです。
20   * 
21   * @author baba
22   */
23  public class StringUtils {
24  
25  	/**
26  	 * 空かどうかを返します。
27  	 * 
28  	 * @param text
29  	 *            文字列
30  	 * @return 空かどうか
31  	 */
32  	public static final boolean isEmpty(final String text) {
33  		return text == null || text.length() == 0;
34  	}
35  
36  	/**
37  	 * ケースインセンシティブで文字列同士が等しいかどうか返します。どちらもnullの場合は、<code>true</code>を返します。
38  	 * 
39  	 * @param target1
40  	 *            文字列1
41  	 * @param target2
42  	 *            文字列2
43  	 * @return ケースインセンシティブで文字列同士が等しいか
44  	 */
45  	public static boolean equalsIgnoreCase(final String target1,
46  			final String target2) {
47  		return (target1 == null) ? (target2 == null) : target1
48  				.equalsIgnoreCase(target2);
49  	}
50  
51  	/**
52  	 * ブランクかどうか返します。
53  	 * 
54  	 * @param str
55  	 *            文字列
56  	 * @return ブランクかどうか
57  	 */
58  	public static boolean isBlank(final String str) {
59  		if (str == null || str.length() == 0) {
60  			return true;
61  		}
62  		for (int i = 0; i < str.length(); i++) {
63  			if (!Character.isWhitespace(str.charAt(i))) {
64  				return false;
65  			}
66  		}
67  		return true;
68  	}
69  
70  	/**
71  	 * ブランクではないかどうか返します。
72  	 * 
73  	 * @param str
74  	 *            文字列
75  	 * @return ブランクではないかどうか
76  	 * @see #isBlank(String)
77  	 */
78  	public static boolean isNotBlank(final String str) {
79  		return !isBlank(str);
80  	}
81  
82  	/**
83  	 * 文字列を置き換えます。
84  	 * 
85  	 * @param text
86  	 *            テキスト
87  	 * @param fromText
88  	 *            置き換え対象のテキスト
89  	 * @param toText
90  	 *            置き換えるテキスト
91  	 * @return 結果
92  	 */
93  	public static final String replace(final String text,
94  			final String fromText, final String toText) {
95  
96  		if (text == null || fromText == null || toText == null) {
97  			return null;
98  		}
99  		final StringBuilder builder = new StringBuilder(100);
100 		int pos = 0;
101 		int pos2 = 0;
102 		while (true) {
103 			pos = text.indexOf(fromText, pos2);
104 			if (pos == 0) {
105 				builder.append(toText);
106 				pos2 = fromText.length();
107 			} else if (pos > 0) {
108 				builder.append(text.substring(pos2, pos));
109 				builder.append(toText);
110 				pos2 = pos + fromText.length();
111 			} else {
112 				builder.append(text.substring(pos2));
113 				break;
114 			}
115 		}
116 		return builder.toString();
117 	}
118 
119 }