java开发中的工具函数(Util)---字符串处理(二)
2016-08-25 14:56
447 查看
日期: 2016-8-25
内容: 字符串处理的基本方法。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字符及其字符串操作工具类
* @author cj(copy jobs)
*
*/
public class StringOperationUtil {
/**
* 功能描述: 将执行的字符串转换为用下划线分割的字符串:
* 比如: HelloWOrld转换之后的字符串就是hello_world;
* @param targetStr 需要转换的字符串
* @return 返回结果
*/
public static String camelToSnake(final String targetStr) {
String convertedStr =
targetStr.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2").replaceAll("([a-z])([A-Z])", "$1_$2");
return convertedStr.toLowerCase();
}
/**
* 功能描述: 判断一个字符串是不是为空,当为空的时候就返回true否则返回false
* @param value 需要判断的字符串
* @return 返回boolean值
*/
public static boolean isEmpty(final String value)
{
return (value == "") || (value.length() == 0);
}
/**
* 功能描述: 判断一个字符串是不是为空,当为空的时候就返回false否则返回true
* @param value
* @return
*/
public static boolean isNotEmpty(final String value)
{
return (value != null) && (value.length() > 0);
}
/**
* 功能描述: 假如字符串不为空或者全部为空格组成的话返回false,假如为空字符串的话返回true
* @param value 判断的字符串
* @return 返回的boolean值
*/
public static boolean isBlankOrWhSpace(final String value)
{
return isEmpty(value) || value.matches("^\\s$");
}
/**
* 判断字符串是不是为空:返回false,否则假如为空或者或者全部由空格组成返回true
* @param value
* @return
*/
public static boolean isBlankOrAllSpace(final String value)
{
return isEmpty(value) || value.matches("^\\s|^[\\s\\p{Zs}]+$");
}
/**
* 判断指定的对象是不是为空:这里没有局限于一个字符串了,而是可以传递一个对象
* @param str
* @return
*/
public static boolean isEmpty(final Object str)
{
return (str == null) || ((str instanceof CharSequence) && str.toString().isEmpty());
}
/**
* 功能描述: 将指定的字符串转换为驼峰式的大小写:比如Hello_world_welcome_to_home---->helloWorldWelcomeToHome;
* @param targetStr 需要转化的字符创
* @return 转换之后的字符串
*/
public static String snakeToCamel(final String targetStr)
{
Pattern pattern = Pattern.compile("_([a-z])");
Matcher matcher = pattern.matcher(targetStr.toLowerCase());
//初始化一个字符容器
StringBuffer sb = new StringBuffer(targetStr.length());
while(matcher.find())
{
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* 将字符串转换为无隐式转换
* @param value
* @return
* @throws ClassCastException
*/
public static String valueOf(final Object value) throws ClassCastException {
if (value == null) {
return null;
} else if (value instanceof CharSequence) {
if (value.toString().isEmpty()) {
return null;
}
return value.toString();
} else {
throw new ClassCastException();
}
}
/**
* 功能描述: 将指定的字符串用指定的连接字符连接起来
* @param slashStr
* @param strings
* @return
*/
public static String combineStr(final String slashStr, final String... args)
{
//初始化一个放字符的容器
StringBuilder sb = new StringBuilder();
for(int i = 0;i<args.length;i++)
{
if(!isBlankOrWhSpace(args[i]))
{
if(i == args.length - 1)
{
//假如是最后一个字符串就直接添加在末尾而不需要添加分割符
sb.append(args[i]);
}
else
{
//在添加字符串之后再添加指定的分割符号
sb.append(args[i]).append(slashStr);
}
}
}
return sb.toString().trim();
}
}
aaa
内容: 字符串处理的基本方法。
一、 J2EE开发过程中的一些字符串操作工具类整理;
1、源代码:
package com.onlyone.www.util.util_char;import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字符及其字符串操作工具类
* @author cj(copy jobs)
*
*/
public class StringOperationUtil {
/**
* 功能描述: 将执行的字符串转换为用下划线分割的字符串:
* 比如: HelloWOrld转换之后的字符串就是hello_world;
* @param targetStr 需要转换的字符串
* @return 返回结果
*/
public static String camelToSnake(final String targetStr) {
String convertedStr =
targetStr.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2").replaceAll("([a-z])([A-Z])", "$1_$2");
return convertedStr.toLowerCase();
}
/**
* 功能描述: 判断一个字符串是不是为空,当为空的时候就返回true否则返回false
* @param value 需要判断的字符串
* @return 返回boolean值
*/
public static boolean isEmpty(final String value)
{
return (value == "") || (value.length() == 0);
}
/**
* 功能描述: 判断一个字符串是不是为空,当为空的时候就返回false否则返回true
* @param value
* @return
*/
public static boolean isNotEmpty(final String value)
{
return (value != null) && (value.length() > 0);
}
/**
* 功能描述: 假如字符串不为空或者全部为空格组成的话返回false,假如为空字符串的话返回true
* @param value 判断的字符串
* @return 返回的boolean值
*/
public static boolean isBlankOrWhSpace(final String value)
{
return isEmpty(value) || value.matches("^\\s$");
}
/**
* 判断字符串是不是为空:返回false,否则假如为空或者或者全部由空格组成返回true
* @param value
* @return
*/
public static boolean isBlankOrAllSpace(final String value)
{
return isEmpty(value) || value.matches("^\\s|^[\\s\\p{Zs}]+$");
}
/**
* 判断指定的对象是不是为空:这里没有局限于一个字符串了,而是可以传递一个对象
* @param str
* @return
*/
public static boolean isEmpty(final Object str)
{
return (str == null) || ((str instanceof CharSequence) && str.toString().isEmpty());
}
/**
* 功能描述: 将指定的字符串转换为驼峰式的大小写:比如Hello_world_welcome_to_home---->helloWorldWelcomeToHome;
* @param targetStr 需要转化的字符创
* @return 转换之后的字符串
*/
public static String snakeToCamel(final String targetStr)
{
Pattern pattern = Pattern.compile("_([a-z])");
Matcher matcher = pattern.matcher(targetStr.toLowerCase());
//初始化一个字符容器
StringBuffer sb = new StringBuffer(targetStr.length());
while(matcher.find())
{
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* 将字符串转换为无隐式转换
* @param value
* @return
* @throws ClassCastException
*/
public static String valueOf(final Object value) throws ClassCastException {
if (value == null) {
return null;
} else if (value instanceof CharSequence) {
if (value.toString().isEmpty()) {
return null;
}
return value.toString();
} else {
throw new ClassCastException();
}
}
/**
* 功能描述: 将指定的字符串用指定的连接字符连接起来
* @param slashStr
* @param strings
* @return
*/
public static String combineStr(final String slashStr, final String... args)
{
//初始化一个放字符的容器
StringBuilder sb = new StringBuilder();
for(int i = 0;i<args.length;i++)
{
if(!isBlankOrWhSpace(args[i]))
{
if(i == args.length - 1)
{
//假如是最后一个字符串就直接添加在末尾而不需要添加分割符
sb.append(args[i]);
}
else
{
//在添加字符串之后再添加指定的分割符号
sb.append(args[i]).append(slashStr);
}
}
}
return sb.toString().trim();
}
}
aaa
2、源代码测试:
package com.onlyone.www.util.util_char; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.onlyone.www.util.util_date.DateOperationUtil; /** * 字符串实体类测试 * @author peixun * */ public class StringOperationUtilTest { //初始化一个变量 public StringOperationUtil stringOperationUtil; @Before public void before() { System.out.println("准备执行测试: "+StringOperationUtil.class); //将变量实例化 stringOperationUtil = new StringOperationUtil(); } @After public void after() { System.out.println(StringOperationUtil.class+"类执行完成!"); } @Test public void testDateOperationUtil() { //调用函数 String test1 = stringOperationUtil.camelToSnake("HelloWorldFuckYoueMother"); //打印输出 System.out.println(test1); } @Test public void testDateOperationUtil_isEmpty() { //调用函数:不为空的情况 boolean test1 = stringOperationUtil.isEmpty("helloworld!"); //为空的情况 boolean test2 = stringOperationUtil.isEmpty(""); //打印输出 System.out.println("字符串不为空的时候应该打印出 false : "+test1+"\n"+"字符串为空的时候应该打印出 true : "+test2); } @Test public void testDateOperationUtil_isNotEmpty() { //调用函数:不为空的情况 boolean test1 = stringOperationUtil.isNotEmpty("helloworld!"); //为空的情况 boolean test2 = stringOperationUtil.isNotEmpty(""); //打印输出 System.out.println("字符串不为空的时候应该打印出 true : "+test1+"\n"+"字符串为空的时候应该打印出 false : "+test2); } @Test public void testDateOperationUtil_isBlankOrWhSpace() { //调用函数:不为空的情况 boolean test1 = stringOperationUtil.isBlankOrWhSpace("helloworld!"); //为全部空格的情况 boolean test2 = stringOperationUtil.isBlankOrWhSpace(" "); //为全部空的情况 boolean test3 = stringOperationUtil.isBlankOrWhSpace(""); //打印输出 System.out.println("字符串不为空的时候应该打印出 false : "+test1+"\n"+"字符串为空格组成的时候应该打印出 false : "+test2+"\n"+"字符串为空的时候应该打印出 true : "+test3); //打印的结果 //字符串不为空的时候应该打印出 false : false //字符串为空格组成的时候应该打印出 false : false //字符串为空的时候应该打印出 true : true } @Test public void testDateOperationUtil_isBlankOrAllSpace() { //调用函数:不为空的情况 boolean test1 = stringOperationUtil.isBlankOrAllSpace("helloworld!"); //为全部空格的情况 boolean test2 = stringOperationUtil.isBlankOrAllSpace(" "); //为全部空的情况 boolean test3 = stringOperationUtil.isBlankOrAllSpace(""); //打印输出 System.out.println("字符串不为空的时候应该打印出 false : "+test1+"\n"+"字符串为空格组成的时候应该打印出 false : "+test2+"\n"+"字符串为空的时候应该打印出 true : "+test3); //打印的结果 //字符串不为空的时候应该打印出 false : false //字符串为空格组成的时候应该打印出 false : true //字符串为空的时候应该打印出 true : true } @Test public void testDateOperationUtil_isEmptyObject() { //声明一个Integer对象: Integer test4 = new Integer(10); //声明一个空的List List<Integer> list1 = new ArrayList<Integer>(); //声明一个空的Map Map<String,Object> map = new HashMap<String,Object>(); //判断函数调用 System.out.println("带值对象调用: "+stringOperationUtil.isEmpty(test4)+"\n不带纸的list调用: "+stringOperationUtil.isEmpty(list1)+"\n不带值得map调用: "+stringOperationUtil.isEmpty(map)); /**************************以下为字符串调用****************************/ //调用函数:不为空的情况 boolean test1 = stringOperationUtil.isEmpty("helloworld!"); //为全部空格的情况 boolean test2 = stringOperationUtil.isEmpty(" "); //为全部空的情况 boolean test3 = stringOperationUtil.isEmpty(""); //打印输出 System.out.println("字符串不为空的时候应该打印出 false : "+test1+"\n"+"字符串为空格组成的时候应该打印出 false : "+test2+"\n"+"字符串为空的时候应该打印出 true : "+test3); //打印的结果 //字符串不为空的时候应该打印出 false : false //字符串为空格组成的时候应该打印出 false : false //字符串为空的时候应该打印出 true : true } @Test public void testDateOperationUtil_snakeToCamel() { String testChar = "Hello_world_welcome_to_home"; //调用函数:不为空的情况 String test1 = stringOperationUtil.snakeToCamel(testChar); //打印输出 System.out.println("将字符串: "+testChar+"\n转换之后的字符串: "+test1); //运行结果 //将字符串: Hello_world_welcome_to_home //转换之后的字符串: helloWorldWelcomeToHome } @Test public void testDateOperationUtil_combineStr() { //调用函数:不为空的情况 String test1 = stringOperationUtil.combineStr("_","hello","world","welcome","to","home"); //打印输出 System.out.println("连接之后的字符串为 : "+test1); //运行结果 //将字符串: Hello_world_welcome_to_home //转换之后的字符串: helloWorldWelcomeToHome } }
相关文章推荐
- java开发中的工具函数(Util)---日期处理(一)
- 谷歌Java开发工具包Guava之String字符串处理工具
- JAVA字符串处理函数列表一览 德仔工作室
- [笔记]java开发常用的工具类函数:时间处理
- java常见工具方法,包括,日期处理,字符串处理,手机号等等
- JAVA字符串处理函数列表一览
- javaweb开发中一些字符串的处理记录
- Java敏感字符串处理工具类(河蟹专用)
- java.util.UnknownFormatConversionException: Conversion: ! 【安卓开发,格式化字符串】
- (4.1.27.6)JNI/NDK开发指南(三)——从字符串处理了解JNI的函数机制
- JAVA字符串处理函数列表一览
- JAVA字符串处理函数列表一览
- java常用流处理工具StreamTool 常见的InputStream流转字符串, 转字节数组等等
- JAVA字符串处理函数列表一览
- JAVA字符串处理函数列表一览
- JAVA字符串处理函数列表一览
- SQL开发应用实例存储过程——各种字符串分拆处理函数
- Java工具类TextUtil(可以进行字符串处理的)
- java常用流处理工具StreamTool 常见的InputStream流转字符串, 转字节数组等等
- 让你提前认识软件开发(10):字符串处理函数及异常保护