工具类,给希望使用的人
public class Convert
{
// 去除右边的空格
public static String rightTrim(String line)
{
int len = line.length();
if (len == 0) return "";
int st = 0;
for (int i = len - 1; i >= 0; i--)
{
if (line.charAt(i) != ' ') {
st = i;
break;
}
}
return line.substring(0, st + 1);
}
//增加num个空格 后缀
public static String increaseChar(String str, int num) {
return increaseChar(str, ' ', num);
}
/**
*
* @Title: increaseChar
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param str 原始字符串
* @param c 后缀字符
* @param num 字符数量
* @return String 返回类型
*/
public static String increaseChar(String str, char c, int num)
{
int limit;
if (num <= 0)
limit = 16;
else
limit = num;
int len = str.length();
for (int i = len; i < limit; i++)
str = str + c;
if (str.length() > limit)
return str.substring(str.length() - limit);
return str;
}
//增加num个前置空格
public static String increaseCharForward(String str, int num) {
return increaseCharForward(str, ' ', num);
}
/**
*
* @Title: increaseCharForward
* @Description: 增加前置字符
* @param str 原始字符串
* @param c 前置字符
* @param num 数量
* @return String 返回类型
*/
public static String increaseCharForward(String str, char c, int num)
{
int limit;
if (num <= 0)
limit = 16;
else
limit = num;
int len = str.length();
for (int i = len; i < limit; i++)
str = c + str;
if (str.length() > limit)
return str.substring(str.length() - limit);
return str;
}
//增加lim个前置0
public static String increaseInt(int num, int lim)
{
String str = "";
int limit;
if (lim <= 0)
limit = 4;
else
limit = lim;
String a = String.valueOf(num);
int len = a.length();
for (int i = 0; i < limit - len; i++)
str = str + "0";
str = str + num;
if (str.length() > limit)
return str.substring(str.length() - limit);
return str;
}
public static String increaseDou(double num, int lim)
{
String str = "";
int limit;
if (lim <= 0)
limit = 16;
else
limit = lim;
String a = String.valueOf(num);
int len = a.length();
for (int i = 0; i < limit - len; i++)
str = str + "0";
str = str + num;
if (str.length() > limit)
return str.substring(str.length() - limit);
return str;
}
public static String increaseLong(long num, int lim)
{
String str = "";
int limit;
if (lim <= 0)
limit = 8;
else
limit = lim;
String a = String.valueOf(num);
int len = a.length();
for (int i = 0; i < limit - len; i++)
str = str + "0";
str = str + num;
if (str.length() > limit)
return str.substring(str.length() - limit);
return str;
}
public static String convertPasswd(String passwd)
{
String line = "";
int len = passwd.length();
for (int i = 2; i < len; i += 2)
{
line = line + "," + Integer.valueOf(passwd.substring(i, i + 2), 16).toString();
}
return line.substring(1);
}
public static String appendStringSize(String orgLine, String addLine, int startIndex, int length, int orgSize) {
return appendStringSize(orgLine, addLine, startIndex, length, orgSize, 0);
}
//
public static String appendStringSize(String orgLine, String addLine,
int startIndex, int length, int orgSize, int alignment)
{
String newLine = orgLine;
int orgLength = countLength(orgLine);
if (orgLength > orgSize) {
newLine = newSubString(orgLine, 0, orgSize);
}
else
{
for (int i = orgLength; i < orgSize; i++)
newLine = newLine + " ";
}
if (addLine != null)
{
if (countLength(addLine) >= length)
{
addLine = newSubString(addLine, 0, length);
}
else
{
int sub = 0;
switch (alignment)
{
case 0:
sub = 0;
break;
case 1:
sub = length - countLength(addLine);
break;
case 2:
sub = (length - countLength(addLine)) / 2;
break;
}
for (int i = countLength(addLine); i < length; i++)
{
if (sub > 0)
{
addLine = " " + addLine;
sub--;
}
else
{
addLine = addLine + " ";
}
}
}
String head = newSubString(newLine, 0, startIndex);
String end = "";
try
{
end = newSubString(newLine, startIndex + length, orgSize);
}
catch (Exception er)
{
er.printStackTrace();
}
newLine = head + addLine + end;
if (countLength(newLine) > orgSize)
{
newLine = newSubString(newLine, 0, orgSize);
}
}
return newLine;
}
public static String newSubString(String line, int start) {
return newSubString(line, start, countLength(line));
}
// 将打不出来的部分赋值给商品名称换行打印项
public static String newSubString(String line, int start, int end)
{
if (start >= end)
return "";
if (countLength(line) <= end - start)
return line;
boolean flag = false;
int startindex = 0;
int endindex = 0;
int length = 0;
for (int i = 0; i < line.length(); i++)
{
char c = line.charAt(i);
if (c > 127) // \177 //ASCII码总共128个 \000 ~ \177 (即0-127)
length += 2;
else
length++;
if (length >= end + 1) {
endindex = i;
break;
}
if ((length >= start + 1) && (!flag)) {
flag = true;
startindex = i;
}
}
if (startindex > endindex) {
return line.substring(startindex);
}
return line.substring(startindex, endindex);
}
/**
* 统计字符串长度
* @param line
* @return
* 只要在0~127中的ASCII中当成一个,大于127当成中文2个长度
*/
public static int countLength(String line)
{
int length = 0;
for (int i = 0; i < line.length(); i++)
{
char c = line.charAt(i);
// if (c > '') {
if (c > 127) //'177' 中文字符算2个长度
length += 2;
else
length++;
}
return length;
}
//判断字符串是否为空
public static boolean isNull(Object obj) {
return obj == null || obj.toString().trim() == "";
}
//判断对象是数字
public static boolean isNumber(Object obj) {
try
{
Double.parseDouble(obj.toString());
return true;
}
catch (Exception e)
{
return false;
}
}
//
public static boolean isShort(Object obj) {
try
{
Short.parseShort(obj.toString());
return true;
}
catch (Exception e)
{
return false;
}
}
public static boolean isInt(Object obj) {
try
{
Integer.parseInt(obj.toString());
return true;
}
catch (Exception e)
{
return false;
}
}
public static boolean isLong(Object obj) {
try
{
Long.parseLong(obj.toString());
return true;
}
catch (Exception e)
{
return false;
}
}
//是否为字母
public static boolean isLetter(char c)
{
if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')))
{
return true;
}
return false;
}
//
public static boolean isDouble(Object obj) {
try
{
Double.parseDouble(obj.toString());
return true;
}
catch (Exception e)
{
return false;
}
}
public static double toDouble(Object obj)
{
try
{
if (obj == null) return 0.0D;
return Double.parseDouble(obj.toString());
}
catch (Exception e)
{
return 0.0D;
}
}
public static int toInt(char c) {
try
{
return Character.getNumericValue(c);
}
catch (Exception e)
{
return 0;
}
}
public static short toShort(Object obj) {
try
{
return (short) (int) toDouble(obj);
}
catch (Exception e)
{
return 0;
}
}
public static int toInt(Object obj) {
try
{
return (int) toDouble(obj);
}
catch (Exception e)
{
return 0;
}
}
public static long toLong(Object obj) {
try
{
return (long) toDouble(obj);
}
catch (Exception e)
{
return 0L;
}
}
/**
* 取得字符串text中的代码
* 以- 取 开头到 -位置的字符
* 以[] 取中间的字符串
*
*/
public static String codeInString(String text, char c)
{
int pos = 0;
if (c == '-')
{
pos = text.indexOf(c);
if (pos < 0)
return "";
return text.substring(0, pos + 1);
}
if ((c == '[') || (c == ']'))
{
pos = text.indexOf('[');
if (pos < 0)
return "";
int len = text.indexOf(']');
if (len < 0)
return "";
return text.substring(pos + 1, len - pos);
}
return text;
}
/**
* add by ssj 2018.11.23
* 判断字符串是否为空
* @param str
* @return
*/
public static boolean stringIsEmpty(String str) {
if (str != null && !"".equals(str.trim())) {
return false;
}
return true;
}
}