一:将数组中的字符转换为一个字符串
1、 将数组中的字符转换为一个字符串
String[3] s={"a","b","c"}
StringUtil.convString(s)="a,b,c" ;
2,static public String converString(String strToConv, String conv)
@param strToConv 要转换的字符串
@param conv 分隔符,默认以逗号分隔
String[3] s={"a","b","c"}
StringUtil.convString(s,"@")="a@b@c"
二:空值处理
3,空值检测:
public static boolean isEmpty(String str)
@param str the String to check, may be null
@return true if the String is empty or null
判断一个字符串是否为空,空格作非空处理。
StringUtils.isEmpty(null) = true ;StringUtils.isEmpty("") = true; StringUtils.isEmpty(" ") = false;
StringUtils.isEmpty("bob") = false ;StringUtils.isEmpty(" bob ") = false ;
4,非空处理:
public static boolean isNotEmpty(String str)
@param str the String to check, may be null
@return true if the String is not empty and not null
判断一个字符串是否非空,空格作非空处理.
StringUtils.isNotEmpty(null) = false; StringUtils.isNotEmpty("") = false ;StringUtils.isNotEmpty(" ") = true;
StringUtils.isNotEmpty("bob") = true ;StringUtils.isNotEmpty(" bob ") = true ;
5, 去空格处理 :返回去除空格后的对象,若是null,返回null
public static String trim(String str)
@param str the String to be trimmed, may be null
@return the trimmed string, null if null String input
格式化一个字符串中的空格,有非空判断处理;
StringUtils.trim(null) = null; StringUtils.trim("") = "" ;StringUtils.trim(" ") = ""; StringUtils.trim("abc") = "abc";
StringUtils.trim(" abc ") = "abc" ;
6,去空格处理 :返回去除空格后的对象,若是null/""/" ",返回null
public static String trimToNull(String str)
@param str the String to be trimmed, may be null
格式化一个字符串中的空格,有非空判断处理,如果为空返回null;
StringUtils.trimToNull(null) = null ;StringUtils.trimToNull("") = null ;StringUtils.trimToNull(" ") = null ;
StringUtils.trimToNull("abc") = "abc"; StringUtils.trimToNull(" abc ") = "abc";
7,去空格处理 :返回去除空格后的对象,若是null/""/" ",返回""
public static String trimToEmpty(String str)
@param str the String to be trimmed, may be null
格式化一个字符串中的空格,有非空判断处理,如果为空返回"";
StringUtils.trimToEmpty(null) = "" ;StringUtils.trimToEmpty("") = "" ;StringUtils.trimToEmpty(" ") = "" ;
StringUtils.trimToEmpty("abc") = "abc" ;StringUtils.trimToEmpty(" abc ") = "abc";
三:字符串比较:
9,public static boolean equals(String str1, String str2)
判断两个字符串是否相等,有非空处理,关注大小写。
StringUtils.equals(null, null) = true; StringUtils.equals(null, "abc") = false; StringUtils.equals("abc", null) = false;
StringUtils.equals("abc", "abc") = true ;StringUtils.equals("abc", "ABC") = false ;
10,public static boolean equalsIgnoreCase(String str1, String str2)
判断两个字符串是否相等,有非空处理。忽略大小写
StringUtils.equalsIgnoreCase(null, null) = true ;StringUtils.equalsIgnoreCase(null, "abc") = false ;
StringUtils.equalsIgnoreCase("abc", null) = false ;StringUtils.equalsIgnoreCase("abc", "abc") = true ;
StringUtils.equalsIgnoreCase("abc", "ABC") = true ;
四:IndexOf 处理
11,public static int indexOf(String str, String searchStr)
@param str the String to check, may be null
@param searchStr the String to find, may be null
@return the first index of the search String
返回要查找的字符串第一次所在位置,有非空处理
StringUtils.indexOf(null, *) = -1; StringUtils.indexOf(*, null) = -1 ;StringUtils.indexOf("", "") = 0 ;
StringUtils.indexOf("aabaabaa", "a") = 0; StringUtils.indexOf("aabaabaa", "b") = 2 ;StringUtils.indexOf("aabaabaa", "ab") = 1;
StringUtils.indexOf("aabaabaa", "") = 0 ;
12,public static int indexOf(String str, String searchStr, int startPos)
@param str the String to check, may be null
@param searchStr the String to find, may be null
@param startPos the start position, negative treated as zero
@return the first index of the search String, -1 if no match or null string input
返回要由指定位置开始查找的字符串第一次所在位置,有非空处理
StringUtils.indexOf(null, *, *) = -1; StringUtils.indexOf(*, null, *) = -1 ;StringUtils.indexOf("", "", 0) = 0 ;
StringUtils.indexOf("aabaabaa", "a", 0) = 0 ;StringUtils.indexOf("aabaabaa", "b", 0) = 2 ;StringUtils.indexOf("aabaabaa", "ab", 0) = 1;
StringUtils.indexOf("aabaabaa", "b", 3) = 5; StringUtils.indexOf("aabaabaa", "b", 9) = -1; StringUtils.indexOf("aabaabaa", "b", -1) = 2;
StringUtils.indexOf("aabaabaa", "", 2) = 2 ;StringUtils.indexOf("abc", "", 9) = 3 ;
五:子字符串处理
13,public static String substring(String str, int start)
@param str the String to get the substring from, may be null
@param start the position to start from, negative means
count back from the end of the String by this many characters
@return substring from start position, null if null String input
返回指定位置开始的字符串中的所有字符, start>0表示从左向右, start<0表示从右向左, start=0则从左第一位开始
StringUtils.substring(null, *) = null ;StringUtils.substring("", *) = "" ;StringUtils.substring("abc", 0) = "abc";
StringUtils.substring("abc", 2) = "c" ;StringUtils.substring("abc", 4) = "" ;StringUtils.substring("abc", -2) = "bc";
StringUtils.substring("abc", -4) = "abc" ;
14,public static String substring(String str, int start, int end)
@param str the String to get the substring from, may be null
@param start the position to start from, negative means
count back from the end of the String by this many characters
@param end the position to end at (exclusive), negative means
count back from the end of the String by this many characters
@return substring from start position to end positon, null if null String input
返回由开始位置到结束位置之间的子字符串,
start>0&&end>0从左开始(包括左)到右结束(不包括右),
start<0&&end<0从右开始(包括右),再向左数到end结束(包括end)
StringUtils.substring(null, *, *) = null ;StringUtils.substring("", * , *) = "";
StringUtils.substring("abc", 0, 2) = "ab" ;StringUtils.substring("abc", 2, 0) = ""; StringUtils.substring("abc", 2, 4) = "c";
StringUtils.substring("abc", 4, 6) = "" ;StringUtils.substring("abc", 2, 2) = ""; StringUtils.substring("abc", -2, -1) = "b";
StringUtils.substring("abc", -4, 2) = "ab" ;
15,public static String substringBefore(String str, String separator)
@param str the String to get a substring from, may be null
@param separator the String to search for, may be null
@return the substring before the first occurance of the separator, null if null String input
Gets the substring before the first occurance of a separator.
The separator is not returned.
A null string input will return null.
An empty ("") string input will return the empty string.
A null separator will return the input string.
返回指定字符串之前的所有字符
StringUtils.substringBefore(null, *) = null; StringUtils.substringBefore("", *) = ""; StringUtils.substringBefore("abc", "a") = "";
StringUtils.substringBefore("abcba", "b") = "a"; StringUtils.substringBefore("abc", "c") = "ab";
StringUtils.substringBefore("abc", "d") = "abc" ;StringUtils.substringBefore("abc", "") = "" ;
StringUtils.substringBefore("abc", null) = "abc" ;
16,public static String substringAfter(String str, String separator)
@param str the String to get a substring from, may be null
@param separator the String to search for, may be null
@return the substring after the first occurance of the separator, null if null String input
Gets the substring after the first occurance of a separator.
The separator is not returned.
A null string input will return null.
An empty ("") string input will return the empty string.
A null separator will return the empty string if the input string is not null.
返回指定字符串之后的所有字符
StringUtils.substringAfter(null, *) = null; StringUtils.substringAfter("", *) = "" ;StringUtils.substringAfter(*, null) = "";
StringUtils.substringAfter("abc", "a") = "bc"; StringUtils.substringAfter("abcba", "b") = "cba";
StringUtils.substringAfter("abc", "c") = ""; StringUtils.substringAfter("abc", "d") = "" ;StringUtils.substringAfter("abc", "") = "abc" ;
17,public static String substringBeforeLast(String str, String separator)
Gets the substring before the last occurance of a separator.
The separator is not returned.
A null string input will return null.
An empty ("") string input will return the empty string.
An empty or null separator will return the input string.
返回最后一个指定字符串之前的所有字符
StringUtils.substringBeforeLast(null, *) = null ;StringUtils.substringBeforeLast("", *) = "";
StringUtils.substringBeforeLast("abcba", "b") = "abc" ;StringUtils.substringBeforeLast("abc", "c") = "ab";
StringUtils.substringBeforeLast("a", "a") = ""; StringUtils.substringBeforeLast("a", "z") = "a";
StringUtils.substringBeforeLast("a", null) = "a" ;StringUtils.substringBeforeLast("a", "") = "a";
18,public static String substringAfterLast(String str, String separator)
Gets the substring after the last occurance of a separator. The separator is not returned.
A null string input will return null. An empty ("") string input will return the empty string.
An empty or null separator will return the empty string if the input string is not null.
返回最后一个指定字符串之后的所有字符
StringUtils.substringAfterLast(null, *) = null; StringUtils.substringAfterLast("", *) = "" ;StringUtils.substringAfterLast(*, "") = "";
StringUtils.substringAfterLast(*, null) = "" ;StringUtils.substringAfterLast("abc", "a") = "bc" ;
StringUtils.substringAfterLast("abcba", "b") = "a"; StringUtils.substringAfterLast("abc", "c") = "";
StringUtils.substringAfterLast("a", "a") = "" ;StringUtils.substringAfterLast("a", "z") = "";
六: Replacing(字符串替换)
19,public static String replace(String text, String repl, String with)
@param text text to search and replace in, may be null
@param repl the String to search for, may be null
@param with the String to replace with, may be null
@return the text with any replacements processed, null if null String input
Replaces all occurances of a String within another String. A null reference passed to this method is a no-op.
以指定字符串替换原来字符串的的指定字符串 ,repl是所给字符串中存在的需要被替换的字符串,with是需要被替换成的新字符串;
StringUtils.replace(null, *, *) = null; StringUtils.replace("", *, *) = ""; StringUtils.replace("aba", null, null) = "aba";
StringUtils.replace("aba", null, null) = "aba"; StringUtils.replace("aba", "a", null) = "aba";
StringUtils.replace("aba", "a", "") = "aba"; StringUtils.replace("aba", "a", "z") = "zbz" ;
20,public static String replace(String text, String repl, String with, int max)
@param text text to search and replace in, may be null
@param repl the String to search for, may be null
@param with the String to replace with, may be null
@param max maximum number of values to replace, or -1 if no maximum
@return the text with any replacements processed,null if null String input
Replaces a String with another String inside a larger String, for the first max values of the search String.
A null reference passed to this method is a no-op.
以指定字符串最大替换原来字符串的的指定字符串,max>0从左向右到max处截止(包括max)替换0到max之间指定的repl为with
StringUtils.replace(null, *, *, *) = null; StringUtils.replace("", *, *, *) = ""; StringUtils.replace("abaa", null, null, 1) = "abaa";
StringUtils.replace("abaa", null, null, 1) = "abaa"; StringUtils.replace("abaa", "a", null, 1) = "abaa" ;
StringUtils.replace("abaa", "a", "", 1) = "abaa"; StringUtils.replace("abaa", "a", "z", 0) = "abaa" ;
StringUtils.replace("abaa", "a", "z", 1) = "zbaa" ;StringUtils.replace("abaa", "a", "z", 2) = "zbza";
StringUtils.replace("abaa", "a", "z", -1) = "zbzz" ;
七,Case conversion(大小写转换)
21,public static String upperCase(String str)
@param str the String to upper case, may be null
@return the upper cased String, null if null String input
Converts a String to upper case as per {@link String#toUpperCase()}. A null input String returns null.
将一个字符串变为大写
StringUtils.upperCase(null) = null; StringUtils.upperCase("") = ""; StringUtils.upperCase("aBc") = "ABC" ;
22,public static String lowerCase(String str)
@param str the String to lower case, may be null
@return the lower cased String, null if null String input
A null input String returns null.
将一个字符串转换为小写
StringUtils.lowerCase(null) = null ;StringUtils.lowerCase("") = "" ;StringUtils.lowerCase("aBc") = "abc" ;
23,public static String capitalize(String str)
将字符串中的首字母大写
StringUtils.capitalize(null) = null; StringUtils.capitalize("") = "" ;
StringUtils.capitalize("cat") = "Cat" ;StringUtils.capitalize("cAt") = "CAt" ;
相关推荐
继承了org.apache.commons.lang3.StringUtils工具类,加入了部分常用方法,使用时直接添加到项目的公共utils下,同时在pom.xml加入依赖: <!-- ...
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 源码,使用的是commons-lang3-3.1包。 以下是StringUtils的各项用法StringUtils的各项用法
org.apache.commons.lang3.StringUtils的jar包,判断字符串为空
commons-lang3-3.1 StringUtils字符串jar包 org.apache.commons.lang3.StringUtils的jar包
StringUtils.java 基于commons-lang-2.5.jar提取 附带使用文档StringUtils.doc
StringUtils方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出NullPointerException,而是做了相应处理,例如,如果输入...
StringUtils jar包,让你更好判断非空字符,对象等等。 记得好评哦
StringUtils(最新的哦)
org.apache.commons.lang3.StringUtils.jar,打开是common-lang3.jar文件,以及使用方法,根据使用方法使用即可,绝对可靠
StringUtils工具类的使用工具类的使用
StringUtils (Lang 2_3 API)
StringUtils.java StringUtils.java
StringUtils 方法的操作对象是 Java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 null 则不会抛出 NullPointerException ,而是做了相应处理,...
包含有org.apache.commons.lang3.StringUtils; org.apache.commons.lang3.reflect.FieldUtils等类