StringUtils.isNotEmpty(String str)和StringUtils.isNotBlank(String str)的使用

自己在项目顺手写了一个用 != ” ” 来判断这个String 字符串是否为空字符串,自己回过头来一看,感觉很二逼。(不知道为啥当时自己没注意到 == 和equals的区别)

StringUtils中有方法判断字符串是否为空,索性看了一下,人家是怎样实现的。

StringUtils的源码地址

176        /**
177         * <p>Checks if a String is empty ("") or null.</p>
178         *
179         * <pre>
180         * StringUtils.isEmpty(null)      = true
181         * StringUtils.isEmpty("")        = true
182         * StringUtils.isEmpty(" ")       = false
183         * StringUtils.isEmpty("bob")     = false
184         * StringUtils.isEmpty("  bob  ") = false
185         * </pre>
186         *
187         * <p>NOTE: This method changed in Lang version 2.0.
188         * It no longer trims the String.
189         * That functionality is available in isBlank().</p>
190         *
191         * @param str  the String to check, may be null
192         * @return <code>true</code> if the String is empty or null
193         */
194        public static boolean isEmpty(String str) {
195            return str == null || str.length() == 0;
196        }
197    
198        /**
199         * <p>Checks if a String is not empty ("") and not null.</p>
200         *
201         * <pre>
202         * StringUtils.isNotEmpty(null)      = false
203         * StringUtils.isNotEmpty("")        = false
204         * StringUtils.isNotEmpty(" ")       = true
205         * StringUtils.isNotEmpty("bob")     = true
206         * StringUtils.isNotEmpty("  bob  ") = true
207         * </pre>
208         *
209         * @param str  the String to check, may be null
210         * @return <code>true</code> if the String is not empty and not null
211         */
212        public static boolean isNotEmpty(String str) {
213            return !StringUtils.isEmpty(str);
214        }
215    
216        /**
217         * <p>Checks if a String is whitespace, empty ("") or null.</p>
218         *
219         * <pre>
220         * StringUtils.isBlank(null)      = true
221         * StringUtils.isBlank("")        = true
222         * StringUtils.isBlank(" ")       = true
223         * StringUtils.isBlank("bob")     = false
224         * StringUtils.isBlank("  bob  ") = false
225         * </pre>
226         *
227         * @param str  the String to check, may be null
228         * @return <code>true</code> if the String is null, empty or whitespace
229         * @since 2.0
230         */
231        public static boolean isBlank(String str) {
232            int strLen;
233            if (str == null || (strLen = str.length()) == 0) {
234                return true;
235            }
236            for (int i = 0; i < strLen; i++) {
237                if ((Character.isWhitespace(str.charAt(i)) == false)) {
238                    return false;
239                }
240            }
241            return true;
242        }
243    
244        /**
245         * <p>Checks if a String is not empty (""), not null and not whitespace only.</p>
246         *
247         * <pre>
248         * StringUtils.isNotBlank(null)      = false
249         * StringUtils.isNotBlank("")        = false
250         * StringUtils.isNotBlank(" ")       = false
251         * StringUtils.isNotBlank("bob")     = true
252         * StringUtils.isNotBlank("  bob  ") = true
253         * </pre>
254         *
255         * @param str  the String to check, may be null
256         * @return <code>true</code> if the String is
257         *  not empty and not null and not whitespace
258         * @since 2.0
259         */
260        public static boolean isNotBlank(String str) {
261            return !StringUtils.isBlank(str);
262        }

猜你喜欢

转载自blog.csdn.net/qdh186/article/details/81232859