📄 stringutils.java
字号:
package com.cownew.ctk.common;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import com.cownew.ctk.common.stringSep.ParserException;
import com.cownew.ctk.common.stringSep.StringSeparator;
public class StringUtils
{
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
/**
* 将byte[]转换为字符串(采用表驱动方式)
* @param is
* @return
*/
public static String byteArrayToString(byte[] is)
{
int i = is.length;
char[] chars = new char[i * 2];
int j = 0;
int k = 0;
while (j < i)
{
int p = is[j++];
chars[k++] = HEX_DIGITS[p >>> 4 & 0xf];
chars[k++] = HEX_DIGITS[p & 0xf];
}
return new String(chars);
}
/**
* 字符串是否为空
* @param string
* @return
*/
public static final boolean isEmpty(String string)
{
return string == null || string.trim().length() == 0;
}
/**
* StringBuffer是否为空
* @param string
* @return
*/
public static final boolean isEmpty(StringBuffer sb)
{
return sb == null || sb.toString().trim().length() == 0;
}
/**
* 得到Throwable栈的字符串格式
* @param throwable
* @return
*/
public static String stackToString(Throwable throwable)
{
StringWriter stringwriter = new StringWriter();
throwable.printStackTrace(new PrintWriter(stringwriter));
return stringwriter.toString();
}
/**
* 使字符串的第一项为大写
* @param string
* @return
*/
public static String firstUpperCase(String string)
{
if (isEmpty(string))
return string;
String first = string.substring(0, 1).toUpperCase();
if (string.length() == 1)
return first;
return first + string.substring(1, string.length());
}
/**
* 使字符串的第一项为小写
* @param string
* @return
*/
public static String firstLowerCase(String string)
{
if (isEmpty(string))
return string;
String first = string.substring(0, 1).toLowerCase();
if (string.length() == 1)
return first;
return first + string.substring(1, string.length());
}
/**
* 给字符串加双引号
* @param string
* @return
*/
public static String doubleQuoted(String string)
{
if (isEmpty(string))
return string;
return "\"" + string + "\"";
}
/**
* 给字符串加单引号
* @param string
* @return
*/
public static String singleQuoted(String string)
{
if (isEmpty(string))
return string;
return "'" + string + "'";
}
/**
* 得到Properties的字符串表示
* @param prop
* @return
*/
public static String propertiesToString(Properties prop)
{
List strList = new ArrayList(prop.size());
Enumeration keyEnum = prop.keys();
while(keyEnum.hasMoreElements())
{
String key = keyEnum.nextElement().toString();
String property = prop.getProperty(key);
String item = StringSeparator.pack(new String[]{key,property});
strList.add(item);
}
return StringSeparator.pack(strList);
}
/**
* 从Properties的字符串表示转化回Properties
* @param value
* @return
* @throws ParserException
*/
public static Properties stringToProperties(String value)
throws ParserException
{
//value有可能为空(当所有配置项都为空的时候),所以要把null转化成空字符串,否则后边会报空指针错误
if(value==null)
{
value = "";
}
Properties props = new Properties();
String[] items = StringSeparator.unPackAsArray(value);
for(int i=0,n=items.length;i<n;i++)
{
String[] keyValue = StringSeparator.unPackAsArray(items[i]);
props.setProperty(keyValue[0], keyValue[1]);
}
return props;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -