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