您的位置:首页 > 其它

StringUtils的简单学习

2016-01-15 16:08 260 查看
package com.hanchao.test;

import org.apache.commons.lang.StringUtils;

/**
* @author liweihan (liweihan@sohu-inc.com)
* @version 1.0 (2016年1月15日 上午11:01:31)
*/
public class StringsUtilsTest {

public static void main(String[] args) {

/**
* org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象,
* 是JDK提供的String类型操作方法的补充,并且是null安全的
* 即如果输入参数String为null则不会抛出NullPointerException,而是做了相应处理。
*
* 都是静态方法。常用的有以下一些:
*/

/**
* 1.public static boolean isEmpty(String str)
* 判断某字符串是否为空,为空的标准是str == null 或 str.length() == 0
*/
System.out.println(StringUtils.isEmpty(null));//true
System.out.println(StringUtils.isEmpty(""));//true
System.out.println(StringUtils.isEmpty(" "));//false
System.out.println(StringUtils.isEmpty("aa"));//false
System.out.println(StringUtils.isEmpty(" aa "));//false
System.out.println("1==========================");

/**
* 2.public static boolean isNotEmpty(String str)
* 判断某字符串是否非空,等于!isEmpty(String str)
*/
System.out.println(StringUtils.isNotEmpty(null));//false
System.out.println(StringUtils.isNotEmpty(""));//false
System.out.println(StringUtils.isNotEmpty(" "));//true
System.out.println(StringUtils.isNotEmpty("aa"));//true
System.out.println(StringUtils.isNotEmpty(" aa "));//true
System.out.println("2==========================");

/**
* 3.public static boolean isBlank(String str)
* 判断某字符串是否为空或长度为0或由空白符(whitespace)构成
* 即 str = null or str.length = 0 or str.trim().length = 0
* 它和isEmpty的区别是都是空字符串时,它认为是isBlank(即true),isEmpty返回false!
*/
System.out.println(StringUtils.isBlank(null));//true
System.out.println(StringUtils.isBlank(""));//true
System.out.println(StringUtils.isBlank(" "));//true [注意isEmpty()是false]
System.out.println(StringUtils.isBlank("aa"));//false
System.out.println(StringUtils.isBlank(" aa "));//false
System.out.println("3==========================");

/**
* 4. public static boolean isNotBlank(String str)
* 判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成,
* 等于!isBlank(String str)
*/
System.out.println(StringUtils.isNotBlank(null));//false
System.out.println(StringUtils.isNotBlank(""));//false
System.out.println(StringUtils.isNotBlank(" "));//false [注意isNotEmpty()是true]
System.out.println(StringUtils.isNotBlank("aa"));//true
System.out.println(StringUtils.isNotBlank(" aa "));//true
System.out.println("4==========================");

/**
* 5.public static String trim(String str)
* 去掉字符串两端的控制符(control characters, char <= 32)
* 如果输入为null则返回null
*/
System.out.println(StringUtils.trim(null)); //null
System.out.println(StringUtils.trim(""));//
System.out.println(StringUtils.trim(" "));//
System.out.println(StringUtils.trim(" \b \t \n \f \r "));//
System.out.println(StringUtils.trim("    \n\tss"));//ss
System.out.println(StringUtils.trim("   ss   "));//ss
System.out.println("5==========================");

/**
* 6.public static String trimToNull(String str)
* 去掉字符串两端的控制符(control characters, char <= 32)
* 如果变为null或"",则返回null
*/
System.out.println(StringUtils.trimToNull(null)); //null
System.out.println(StringUtils.trimToNull(""));//null
System.out.println(StringUtils.trimToNull(" "));//null
System.out.println(StringUtils.trimToNull(" \b \t \n \f \r "));//null
System.out.println(StringUtils.trimToNull("    \n\tss  \b"));//ss
System.out.println(StringUtils.trimToNull("   ss   "));//ss
System.out.println("6==========================");

/**
* 7.public static String trimToEmpty(String str)
* 去掉字符串两端的控制符(control characters, char <= 32)
* 如果变为null或"",则返回""
*/
System.out.println(StringUtils.trimToEmpty(null)); //
System.out.println(StringUtils.trimToEmpty(""));//
System.out.println(StringUtils.trimToEmpty(" "));//
System.out.println(StringUtils.trimToEmpty(" \b \t \n \f \r "));//
System.out.println(StringUtils.trimToEmpty("    \n\tss  \b"));//ss
System.out.println(StringUtils.trimToEmpty("   ss   "));//ss
System.out.println("7==========================");

/**
* 8.public static boolean equals(String str1, String str2)
* 比较两个字符串是否相等,如果两个均为空则也认为相等
*/
System.out.println(StringUtils.equals("", ""));//true
System.out.println(StringUtils.equals(null, null));//true
System.out.println(StringUtils.equals("ss", null));//false
System.out.println(StringUtils.equals("AA", "aa"));//false
System.out.println("8==========================");

/**
* 9.public static boolean equalsIgnoreCase(String str1, String str2)
* 比较两个字符串是否相等,不区分大小写,如果两个均为空则也认为相等
*/
System.out.println(StringUtils.equalsIgnoreCase("", ""));//true
System.out.println(StringUtils.equalsIgnoreCase(null, null));//true
System.out.println(StringUtils.equalsIgnoreCase("ss", null));//false
System.out.println(StringUtils.equalsIgnoreCase("AA", "aa"));//true
System.out.println("9==========================");

/**
* 10.public static int indexOf(String str, char searchChar)
* 返回字符searchChar在字符串str中第一次出现的位置。
* 如果searchChar没有在str中出现则返回-1,
* 如果str为null或"",则也返回-1
*
* 	说明:同理!
public static int lastIndexOf(String str, char searchChar)
*/
System.out.println(StringUtils.indexOf("abcdefg", 'b'));//1
System.out.println(StringUtils.indexOf(null, 'b'));//-1
System.out.println(StringUtils.indexOf("", 'a'));//-1
System.out.println("10==========================");

/**
* 11.public static int indexOf(String str, char searchChar, int startPos)
* 返回字符searchChar从startPos开始在字符串str中第一次出现的位置。
* 如果从startPos开始searchChar没有在str中出现则返回-1,
* 如果str为null或"",则也返回-1
*
* 说明:同理!
* public static int lastIndexOf(String str, char searchChar, int startPos)
*/
System.out.println(StringUtils.indexOf("abcdefbg", 'b',2));//6
System.out.println(StringUtils.indexOf(null, 'b',2));//-1
System.out.println(StringUtils.indexOf("", 'a',2));//-1
System.out.println("11==========================");

/**
* 12.public static int indexOf(String str, String searchStr)
* 返回字符串searchStr在字符串str中第一次出现的位置。
如果str为null或searchStr为null则返回-1,
如果searchStr为"",且str为不为null,则返回0,☆
如果searchStr不在str中,则返回

说明:同理!
public static int lastIndexOf(String str, String searchStr)
*/
System.out.println(StringUtils.indexOf("abcdefg", "b"));//1
System.out.println(StringUtils.indexOf("abcdefg", "cde"));//2
System.out.println(StringUtils.indexOf("abcdefg", null));//-1
System.out.println(StringUtils.indexOf(null, "aa"));//-1
System.out.println(StringUtils.indexOf("abcdefg", ""));//0
System.out.println("12==========================");

/**
* 13.public static int indexOf(String str, String searchStr, int startPos)
* 返回字符串searchStr从startPos开始在字符串str中第一次出现的位置。
* 如果searchStr为"",且str为不为null,则返回startPos
* 如果startPos为负数或0,则默认是第一次出现的位置
*
* 说明:同理!
* public static int lastIndexOf(String str, String searchStr, int startPos)
*/
System.out.println(StringUtils.indexOf("abcdefg", "b",1));//1
System.out.println(StringUtils.indexOf("abcdefgcde", "cde",1));//2
System.out.println(StringUtils.indexOf("abcdefgcde", "d",-1));//3
System.out.println(StringUtils.indexOf("abcdefgcde", "d",0));//3
System.out.println(StringUtils.indexOf("abcdefgcde", "d",20));//-1
System.out.println(StringUtils.indexOf("abcdefg", null,2));//-1
System.out.println(StringUtils.indexOf(null, "aa",2));//-1
System.out.println(StringUtils.indexOf("abcdefg", "",2));//2
System.out.println("13==========================");

/**
* 14.public static int ordinalIndexOf(String str, String searchStr, int ordinal)
返回字符串searchStr在字符串str中第ordinal次出现的位置。
如果str=null或searchStr=null或ordinal<=0则返回-1
*/
System.out.println(StringUtils.ordinalIndexOf(null, "aa", 2));//-1
System.out.println(StringUtils.ordinalIndexOf("abcdefgabc", null, 2));//-1
System.out.println(StringUtils.ordinalIndexOf("abcdefgabc", "aa", -2));//-1
System.out.println(StringUtils.ordinalIndexOf("", "", 2));//0
System.out.println(StringUtils.ordinalIndexOf("abcdefabc", "ab", 1));//0
System.out.println(StringUtils.ordinalIndexOf("abcdefabc", "ab", 2));//6
System.out.println(StringUtils.ordinalIndexOf("abcdefabc", "a", 1));//0
System.out.println(StringUtils.ordinalIndexOf("abcdefabc", "a", 2));//6
System.out.println(StringUtils.ordinalIndexOf("", "aa", 2));//-1
System.out.println(StringUtils.ordinalIndexOf("aaaaa", "", 1));//0
System.out.println("14==========================");

/**
* 15.public static boolean contains(String str, char searchChar)
* 判断字符串str中是否包含字符searchChar。
如果str为null或"",返回false;
如果searchChar不在str中,返回false。
*/
System.out.println(StringUtils.contains("abc", 'a'));//true
System.out.println(StringUtils.contains(null, 'a'));//false
System.out.println(StringUtils.contains("", 'a'));//false
System.out.println("15==========================");

/**
* 16.public static boolean contains(String str, String searchStr)
* 判断字符串str是否包含字符串searchStr。
* 如果str为null或searchStr为null,返回false
* 如果str为"",并且searchStr为"",返回true
*
* 说明:同理!!(只是不区分大小写!)
* public static boolean containsIgnoreCase(String str, String searchStr)
*/
System.out.println(StringUtils.contains("abc", "ab"));//true
System.out.println(StringUtils.contains("abc", "ac"));//false
System.out.println(StringUtils.contains(null, "ac"));//false
System.out.println(StringUtils.contains("abc", null));//false
System.out.println(StringUtils.contains("", ""));//true
System.out.println("16==========================");

/**
* 17.public static String substring(String str, int start)
* 得到字符串str的子串。
如果start小于0,位置是从后往前数的第|start|个
如果str为null或"",则返回它本身
*/
System.out.println(StringUtils.substring(null, 1));//null
System.out.println(StringUtils.substring("", 1));//
System.out.println(StringUtils.substring("asdf", 0));//asdf
System.out.println(StringUtils.substring("asdf", 1));//sdf
System.out.println(StringUtils.substring("asdf", 3));//f
System.out.println(StringUtils.substring("asdf", -1));//f
System.out.println(StringUtils.substring("asdf", -2));//df
System.out.println(StringUtils.substring("asdf", -3));//sdf
System.out.println(StringUtils.substring("asdf", -8));//asdf
System.out.println("17==========================");

/**
* 18.public static String substring(String str, int start, int end)
得到字符串str的子串。
如果start小于0,位置是从后往前数的第|start|个,
如果end小于0,位置是从后往前数的第|end|个,
如果str为null或"",则返回它本身
*/
System.out.println(StringUtils.substring(null, 0,1));//null
System.out.println(StringUtils.substring("", 0,1));//
System.out.println(StringUtils.substring("asdf", 0,2));//as
System.out.println(StringUtils.substring("asdf", 0,-1));//asd
System.out.println(StringUtils.substring("asdf", 2,-1));//d
System.out.println(StringUtils.substring("asdf", 2,-2));//
System.out.println(StringUtils.substring("asdf", 3,2));//
System.out.println(StringUtils.substring("asdf", -1,-3));//
System.out.println(StringUtils.substring("asdf", -3,-1));//sd
System.out.println(StringUtils.substring("asdf", -8,5));//asdf
System.out.println("18==========================");

/**
* 19.public static String mid(String str, int pos, int len)
* 得到字符串str从pos开始len长度的子串。
如果str为null或为"",则返回它本身
如果len小于0或pos大于srt的长度,则返回""
如果pos小于0,则pos设为0
*/
System.out.println(StringUtils.mid(null, 0, 1));//null
System.out.println(StringUtils.mid("", 0, 1));//
System.out.println(StringUtils.mid("abc", 0, -1));//
System.out.println(StringUtils.mid("abc", 6, 1));//
System.out.println(StringUtils.mid("abcd", 0, 2));//ab
System.out.println(StringUtils.mid("abcd", -3, 2));//ab
System.out.println("19==========================");

/**
* 20.public static String join(Object[] array)
* 把数组中的元素连接成一个字符串返回。
*/
System.out.println(StringUtils.join(null)); //null
System.out.println(StringUtils.join(new String[]{}));//
System.out.println(StringUtils.join(new String[]{"aa","bb"}));//aabb
System.out.println("20==========================");

/**
* 21. public static int countMatches(String str, String sub)
*  计算字符串sub在字符串str中出现的次数。
如果str为null或"",则返回0
*/
System.out.println(StringUtils.countMatches(null, "a"));//0
System.out.println(StringUtils.countMatches("", "a"));//0
System.out.println(StringUtils.countMatches("abcd", "a"));//1
System.out.println(StringUtils.countMatches("abcdabc", "a"));//2
System.out.println(StringUtils.countMatches("abcd", "f"));//0
System.out.println(StringUtils.countMatches("abc ", ""));//0
System.out.println(StringUtils.countMatches("abc ", " "));//1
System.out.println(StringUtils.countMatches("abc", null));//0
System.out.println("21==========================");

/**
* 22.public static String swapCase(String str)
* 把字符串中的字符大写转换为小写,小写转换为大写。
*/
System.out.println(StringUtils.swapCase(null));//null
System.out.println(StringUtils.swapCase(""));//
System.out.println(StringUtils.swapCase("Hello tom!"));//hELLO TOM!
System.out.println("22==========================");

/**
* 23.public static String repeat(String str, int repeat)
* 重复字符串repeat次,组合成一个新串返回。
* 如果字符串str为null或"",则返回它本身
* 如果repeat小于0,则返回""
*/
System.out.println(StringUtils.repeat(null, 2));//null
System.out.println(StringUtils.repeat("", 3));//
System.out.println(StringUtils.repeat("a", 3));//aaa
System.out.println(StringUtils.repeat("abc", 2));//abcabc
System.out.println(StringUtils.repeat("a", -2));//
System.out.println("23==========================");

/**
* 24.public static String replace(String text, String repl, String with)
* 在字符串text中用with代替repl,替换所有
*/
System.out.println(StringUtils.replace(null, "ab", "xx")); //null
System.out.println(StringUtils.replace("abcdef", null, "xx"));//abcdef
System.out.println(StringUtils.replace("abcdef", "ab", null));//abcdef
System.out.println(StringUtils.replace("abcdefab,hanchao","ab","xxx"));//xxxcdefxxx,hanchao
System.out.println("24==========================");

/**
* 参考:下面文章比较全面!
* http://blog.sina.com.cn/s/blog_4550f3ca0100qrsd.html */
}

}


本文出自 “我的JAVA世界” 博客,请务必保留此出处http://hanchaohan.blog.51cto.com/2996417/1735367
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: