📄 stringparse.java
字号:
package com.hope.itissue.shared;
import java.io.*;
import java.text.*;
import java.util.*;
/**
* 针对字符串进行操作转换,所有方法均为静态方法。
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: hope</p>
* <p>author zyt</p>
* <p>version 1.0</p>
*/
public class StringParse {
/**
* 将以ISO-8859-1编码的字符串转换为GBK编码,解决中文乱码问题。
* @param str String 需要转换的字符串
* @return String 转换后的字符串
*/
public static String ISO2GBK(String str) {
if (str == null || str.length() == 0) {
return "";
}
try {
byte[] b = str.getBytes("ISO-8859-1");
return new String(b, "GBK");
} catch (UnsupportedEncodingException e) {
return str;
}
}
public static String chkStr(String str) {
return ISO2GBK(str);
}
/**
* 替换字符串中的单引号,适应数据库中的SQL语句
* @param inStr String 需要处理的字符串
* @return String 处理后的字符串
*/
public static String parseSingleQuote(String inStr) {
if (inStr == null || inStr.length() == 0) {
return "";
} else {
StringBuffer result = new StringBuffer("");
char cc;
for (int i = 0; i < inStr.length(); i++) {
cc = inStr.charAt(i);
if (cc == '\'') {
result.append('\'');
}
result.append(cc);
}
return result.toString();
}
}
/**
* 将当前字符串转换为yyyy-mm-dd格式的日期,转换失败返回原字符串
* @param str String 需要转换的字符串
* @return String 转换后的日期字符串
*/
public static String getDate(String str) {
if (str == null || str.length() == 0) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(str);
return sdf.format(date);
} catch (ParseException e) {
return str;
}
}
/**
* 将当前字符串转换为yyyy-mm-dd hh:mm:ss格式的日期时间,转换失败返回原字符串
* @param str String 需要转换的字符串
* @return String 转换后的日期时间字符串
*/
public static String getDateTime(String str) {
if (str == null || str.length() == 0) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = sdf.parse(str);
return sdf.format(date);
} catch (ParseException e) {
return str;
}
}
/**
* 将当前字符串转换为hh:mm:ss格式的时间,转换失败返回原字符串
* @param str String 需要转换的字符串
* @return String 转换后的时间字符串
*/
public static String getTime(String str) {
if (str == null || str.length() == 0) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
try {
Date date = sdf.parse(str);
return sdf.format(date);
} catch (ParseException e) {
return str;
}
}
/**
* 将字符串中的html标记进行转义,如:"<"转换为"<",主要对以下标记进行处理
* "<",">","\","&","\n"
* @param inStr String 需要处理的字符串
* @return String 处理后的字符串
*/
public static String htmlEncode(String inStr) {
if (inStr == null || inStr.length() == 0) {
return "";
} else {
StringBuffer result = new StringBuffer("");
for (int i = 0; i < inStr.length(); i++) {
switch (inStr.charAt(i)) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '\\':
result.append("\\\\");
break;
case '&':
result.append("&");
break;
case '\n':
result.append("<br>");
break;
default:
result.append(inStr.substring(i, i + 1));
break;
}
}
return result.toString();
}
}
/**
* 将值为null的字符串对象,转换为空字符串""。
* @param inStr String 需要处理的字符串
* @return String 处理后的字符串
*/
public static String null2Empty(String inStr) {
if (inStr == null || inStr.length() == 0) {
return "";
} else {
return inStr;
}
}
/**
* 判断字符串是否为Double类型,如果是返回此字符串,否则返回0
* @param str String
* @return String
*/
public static String chkNumber(String str) {
if (str == null || str.length() == 0) {
return "0";
}
try {
new Double(str);
return str;
} catch (NumberFormatException e) {
return "0";
}
}
/**
* 将字符串格式化为两位小数的数字,有异常返回0.00
* @param str String
* @return String
*/
public static String formatStr(String str) {
double num = Double.parseDouble(chkNumber(str));
DecimalFormat df = new DecimalFormat("0.00");
return df.format(num);
}
/**
* 将字符串格式化为数字,有异常返回0
* @param str String
* @param bitSize int 小数位数
* @return String
*/
public static String formatStr(String str, int bitSize) {
double num = Double.parseDouble(chkNumber(str));
DecimalFormat df = new DecimalFormat();
switch (bitSize) {
case 0:
Double d = new Double(num);
long n = d.longValue();
if (d.doubleValue() - n >= 0.5d) {
n++;
}
//System.out.println("n: " + n);
num = new Long(n).doubleValue();
df.applyPattern("0");
break;
case 1:
df.applyPattern("0.0");
break;
case 2:
df.applyPattern("0.00");
break;
case 3:
df.applyPattern("0.000");
break;
default:
df.applyPattern("0.0000");
break;
}
return df.format(num);
}
public static String chkStr1(String str) {
if (str != null && str.equals("1")) {
return "项目经理";
} else if (str != null && str.equals("2")) {
return "监理";
} else if (str != null && str.equals("3")) {
return "总监办";
} else if (str != null && str.equals("4")) {
return "业主";
} else {
return "";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -