`

StringUtils

阅读更多
StringUtils



package com.java.study.test.unit7;

/**
 * String常用工具类
 */
public class StringUtils {

	/**
	 * 为空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	/**
	 * 非空
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}
    
	/**
	 * 为空,判断空字符
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0)
			return true;
		for (int i = 0; i < strLen; i++)
			if (!Character.isWhitespace(str.charAt(i)))
				return false;

		return true;
	}

	public static boolean isNotBlank(String str) {
		return !isBlank(str);
	}

	public static String trim(String str) {
		return str != null ? str.trim() : null;
	}

	public static String trimToNull(String str) {
		String ts = trim(str);
		return isEmpty(ts) ? null : ts;
	}

	public static String trimToEmpty(String str) {
		return str != null ? str.trim() : "";
	}

	public static String strip(String str) {
		return strip(str, null);
	}

	public static String stripToNull(String str) {
		if (str == null) {
			return null;
		} else {
			str = strip(str, null);
			return str.length() != 0 ? str : null;
		}
	}

	public static String stripToEmpty(String str) {
		return str != null ? strip(str, null) : "";
	}

	public static String strip(String str, String stripChars) {
		if (isEmpty(str)) {
			return str;
		} else {
			str = stripStart(str, stripChars);
			return stripEnd(str, stripChars);
		}
	}

	public static String stripStart(String str, String stripChars) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0)
			return str;
		int start = 0;
		if (stripChars == null) {
			for (; start != strLen && Character.isWhitespace(str.charAt(start)); start++)
				;
		} else {
			if (stripChars.length() == 0)
				return str;
			for (; start != strLen
					&& stripChars.indexOf(str.charAt(start)) != -1; start++)
				;
		}
		return str.substring(start);
	}

	public static String stripEnd(String str, String stripChars) {
		int end;
		if (str == null || (end = str.length()) == 0)
			return str;
		if (stripChars == null) {
			for (; end != 0 && Character.isWhitespace(str.charAt(end - 1)); end--)
				;
		} else {
			if (stripChars.length() == 0)
				return str;
			for (; end != 0 && stripChars.indexOf(str.charAt(end - 1)) != -1; end--)
				;
		}
		return str.substring(0, end);
	}

	public static String[] stripAll(String strs[]) {
		return stripAll(strs, null);
	}

	public static String[] stripAll(String strs[], String stripChars) {
		int strsLen;
		if (strs == null || (strsLen = strs.length) == 0)
			return strs;
		String newArr[] = new String[strsLen];
		for (int i = 0; i < strsLen; i++)
			newArr[i] = strip(strs[i], stripChars);

		return newArr;
	}

	public static boolean equals(String str1, String str2) {
		return str1 != null ? str1.equals(str2) : str2 == null;
	}

	public static boolean equalsIgnoreCase(String str1, String str2) {
		return str1 != null ? str1.equalsIgnoreCase(str2) : str2 == null;
	}

	public static int indexOf(String str, char searchChar) {
		if (isEmpty(str))
			return -1;
		else
			return str.indexOf(searchChar);
	}

	public static int indexOf(String str, char searchChar, int startPos) {
		if (isEmpty(str))
			return -1;
		else
			return str.indexOf(searchChar, startPos);
	}

	public static int indexOf(String str, String searchStr) {
		if (str == null || searchStr == null)
			return -1;
		else
			return str.indexOf(searchStr);
	}

}



分享到:
评论

相关推荐

    StringUtils的各项用法

    《StringUtils的深度解析与应用》 在Java编程中,处理字符串是常见的任务,Apache Commons Lang库中的StringUtils类提供了丰富的字符串操作方法,极大地提高了开发效率。本文将深入探讨StringUtils的几个重要功能,...

    org.apache.commons.lang3.StringUtils.jar.rar

    在给定的标题 "org.apache.commons.lang3.StringUtils.jar.rar" 中,我们可以看到这个压缩包包含的是 `StringUtils.jar`,实际上它是一个 `common-lang3.jar` 文件的别名。这个 JAR 包是 Apache Commons Lang 项目的...

    org.apache.commons.lang3.StringUtils.jar

    java获取客户端ip(经过多次代理)提示StringUtils cannot be resolved 需要先 import org.apache.commons.lang3.StringUtils; /* 内含 common-lang3.jar commons-lang3-3.9-bin.zip commons-lang3-3.9-src.zip ...

    自定封装StringUtils常用方法

    继承了org.apache.commons.lang3.StringUtils工具类,加入了部分常用方法,使用时直接添加到项目的公共utils下,同时在pom.xml加入依赖: &lt;!-- ...

    StringUtils(最新)

    `StringUtils` 是 Apache Commons Lang 库中的一个核心类,提供了大量关于字符串操作的实用方法,旨在作为 Java 核心库的扩展。这个库在 `commons-lang-2.5` 版本中是最新的,尽管现在可能有更新的版本,但这个版本...

    stringUtils的jar包

    org.apache.commons.lang3.StringUtils的jar包,判断字符串为空

    StringUtils工具类的使用

    `StringUtils`工具类是Java开发中非常常用的一个类库,主要提供了一系列静态方法来处理字符串。这个工具类在处理字符串的常见操作时提供了很大的便利,比如数组转字符串、空值检测、非空处理以及空格处理等。接下来...

    StringUtils API 使用方法

    `StringUtils` API 是 Apache Commons Lang 库中的一个实用工具类,专门为处理 `java.lang.String` 对象提供了丰富的静态方法。这个库是对 Java 标准库中的 `String` 类方法的一个扩展,尤其在处理 `null` 和空白...

    StringUtils源码及使用文档

    `StringUtils`是Apache Commons Lang库中的一个核心工具类,它提供了大量的静态方法,用于处理字符串。这个类在Java开发中非常常见,因为它弥补了Java标准库中对字符串操作的不足。`StringUtils`类包含了多种字符串...

    StringUtils jar包

    `StringUtils` jar包是Java开发中的一个实用工具库,它为处理字符串提供了许多方便的方法。这个库主要由Apache Commons Lang项目提供,这是一个广泛使用的开源组件,旨在补充Java标准库中对于字符串操作的功能不足。...

    StringUtils (Lang 2_3 API)

    `StringUtils`是Apache Commons Lang库中的一个核心工具类,它提供了大量的静态方法,用于处理字符串。这个库在Java开发中非常常见,因为它弥补了Java标准库中对字符串操作的不足。`StringUtils`类包含了多种实用...

    commons-lang3-3.1 StringUtils字符串jar包

    commons-lang3-3.1 StringUtils字符串jar包 org.apache.commons.lang3.StringUtils的jar包

    StringUtils 字符串常用工具

    在Java编程语言中,`StringUtils` 是一个非常实用的工具类,它提供了大量关于字符串操作的方法,极大地简化了对字符串的处理。这个类通常在处理字符串时提高代码的可读性和效率,尤其在处理字符串的空值、拼接、分割...

    StringUtils函数全集

    ### StringUtils函数全集详解 #### 一、简介 在Java编程语言中,处理字符串是非常常见的需求之一。为了方便开发者高效地进行字符串操作,Apache Commons Lang库提供了一个强大的工具类——`StringUtils`。此工具类...

    StringUtils的用法

    \n\t\f\r") = "" //所有控制字符都被移除 StringUtils.trim("bob") = "bob" StringUtils.trim(" bob ") = "bob" 6. public static String trimToNull(String str) 和 trim() 类似,但结果为 null 时返回 null,而...

    B4A - StringUtils.rar_android_b4a_basic_strings_utilities

    "StringUtils.rar_android_b4a_basic_strings_utilities"这个压缩包,显然专注于介绍B4A中处理字符串的实用工具。在本教程中,我们将深入探讨B4A中的字符串处理函数和技巧,这对于任何想要提高其B4A编程技能的开发者...

Global site tag (gtag.js) - Google Analytics