📄 stringutilexpress.java
字号:
/*
* @(#)StringUtil.java 1.0 2005-10-19
*
* Copyright 2003 - 2006 BeanSoft Studio. All rights reserved.
*/
package beansoft.jsp;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Date;
import beansoft.util.OS;
/**
* StringUtilExpress, 字符串工具类, 一些方便的字符串工具方法. 不包含 Servlet 处理工具.
* Used by Applet.
*
* Dependencies: None.
*
* @author beansoft
* @version 1.1 2005-10-19
*/
public class StringUtilExpress {
/**
* 获取类路径中的资源文件的物理文件路径.
* NOTE: 仅在 Win32 平台下测试通过开发.
* @date 2005.10.16
* @param resourcePath 资源路径
* @return 配置文件路径
*/
public static String getRealFilePath(String resourcePath) {
java.net.URL inputURL = StringUtilExpress.class
.getResource(resourcePath);
String filePath = inputURL.getFile();
// For windows platform, the filePath will like this:
// /E:/Push/web/WEB-INF/classes/studio/beansoft/smtp/MailSender.ini
// So must remove the first /
if(OS.isWindows() && filePath.startsWith("/")) {
filePath = filePath.substring(1);
}
return filePath;
}
/**
* 将字符串转换为 int.
*
* @param input
* 输入的字串
* @date 2005-07-29
* @return 结果数字
*/
public static int parseInt(String input) {
try {
return Integer.parseInt(input);
} catch (Exception e) {
// TODO: handle exception
}
return 0;
}
/**
* 格式化日期到日时分秒时间格式的显示. d日 HH:mm:ss
*
* @return - String 格式化后的时间
*/
public static String formatDateToDHMSString(java.util.Date date) {
if (date == null) {
return "";
}
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
"d日 HH:mm:ss");
return dateFormat.format(date);
}
/**
* 格式化日期到时分秒时间格式的显示.
*
* @return - String 格式化后的时间
*/
public static String formatDateToHMSString(java.util.Date date) {
if (date == null) {
return "";
}
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
"HH:mm:ss");
return dateFormat.format(date);
}
/**
* 将时分秒时间格式的字符串转换为日期.
*
* @param input
* @return
*/
public static Date parseHMSStringToDate(String input) {
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
"HH:mm:ss");
try {
return dateFormat.parse(input);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 格式化日期到 Mysql 数据库日期格式字符串的显示.
*
* @return - String 格式化后的时间
*/
public static String formatDateToMysqlString(java.util.Date date) {
if (date == null) {
return "";
}
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date);
}
/**
* 将 Mysql 数据库日期格式字符串转换为日期.
*
* @param input
* @return
*/
public static Date parseStringToMysqlDate(String input) {
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
return dateFormat.parse(input);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 返回时间字符串, 可读形式的, M月d日 HH:mm 格式. 2004-09-22, LiuChangjiong
*
* @return - String 格式化后的时间
*/
public static String formatDateToMMddHHmm(java.util.Date date) {
if (date == null) {
return "";
}
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
"M月d日 HH:mm");
return dateFormat.format(date);
}
/**
* 返回时间字符串, 可读形式的, yy年M月d日HH:mm 格式. 2004-10-04, LiuChangjiong
*
* @return - String 格式化后的时间
*/
public static String formatDateToyyMMddHHmm(java.util.Date date) {
if (date == null) {
return "";
}
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
"yy年M月d日HH:mm");
return dateFormat.format(date);
}
/**
* 生成一个 18 位的 yyyyMMddHHmmss.SSS 格式的日期字符串.
*
* @param date
* Date
* @return String
*/
public static String genTimeStampString(Date date) {
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
"yyyyMMddHHmmss.SSS");
return df.format(date);
}
/**
* 将字符串 source 中的 oldStr 替换为 newStr, 并以大小写敏感方式进行查找
*
* @param source
* 需要替换的源字符串
* @param oldStr
* 需要被替换的老字符串
* @param newStr
* 替换为的新字符串
*/
public static String replace(String source, String oldStr, String newStr) {
return replace(source, oldStr, newStr, true);
}
/**
* 将字符串 source 中的 oldStr 替换为 newStr, matchCase 为是否设置大小写敏感查找
*
* @param source
* 需要替换的源字符串
* @param oldStr
* 需要被替换的老字符串
* @param newStr
* 替换为的新字符串
* @param matchCase
* 是否需要按照大小写敏感方式查找
*/
public static String replace(String source, String oldStr, String newStr,
boolean matchCase) {
if (source == null) {
return null;
}
// 首先检查旧字符串是否存在, 不存在就不进行替换
if (source.toLowerCase().indexOf(oldStr.toLowerCase()) == -1) {
return source;
}
int findStartPos = 0;
int a = 0;
while (a > -1) {
int b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = source;
str2 = str1.toLowerCase();
str3 = oldStr;
str4 = str3.toLowerCase();
if (matchCase) {
strA = str1;
strB = str3;
} else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, findStartPos);
if (a > -1) {
b = oldStr.length();
findStartPos = a + b;
StringBuffer bbuf = new StringBuffer(source);
source = bbuf.replace(a, a + b, newStr) + "";
// 新的查找开始点位于替换后的字符串的结尾
findStartPos = findStartPos + newStr.length() - b;
}
}
return source;
}
/**
* 清除字符串结尾的空格.
*
* @param input
* String 输入的字符串
* @return 转换结果
*/
public static String trimTailSpaces(String input) {
if (isEmpty(input)) {
return "";
}
String trimedString = input.trim();
if (trimedString.length() == input.length()) {
return input;
}
return input.substring(0, input.indexOf(trimedString)
+ trimedString.length());
}
/**
* Change the null string value to "", if not null, then return it self, use
* this to avoid display a null string to "null".
*
* @param input
* the string to clear
* @return the result
*/
public static String clearNull(String input) {
return isEmpty(input) ? "" : input;
}
/**
* Return the limited length string of the input string (added at:April 10,
* 2004).
*
* @param input
* String
* @param maxLength
* int
* @return String processed result
*/
public static String limitStringLength(String input, int maxLength) {
if (isEmpty(input))
return "";
if (input.length() <= maxLength) {
return input;
} else {
return input.substring(0, maxLength - 3) + "...";
}
}
/**
* 将字符串转换为一个 JavaScript 的 alert 调用. eg: htmlAlert("What?"); returns
* <SCRIPT language="JavaScript">alert("What?")</SCRIPT>
*
* @param message
* 需要显示的信息
* @return 转换结果
*/
public static String scriptAlert(String message) {
return "<SCRIPT language=\"JavaScript\">alert(\"" + message
+ "\");</SCRIPT>";
}
/**
* 将字符串转换为一个 JavaScript 的 document.location 改变调用. eg: htmlAlert("a.jsp");
* returns <SCRIPT
* language="JavaScript">document.location="a.jsp";</SCRIPT>
*
* @param url
* 需要显示的 URL 字符串
* @return 转换结果
*/
public static String scriptRedirect(String url) {
return "<SCRIPT language=\"JavaScript\">document.location=\"" + url
+ "\";</SCRIPT>";
}
/**
* 返回脚本语句 <SCRIPT language="JavaScript">history.back();</SCRIPT>
*
* @return 脚本语句
*/
public static String scriptHistoryBack() {
return "<SCRIPT language=\"JavaScript\">history.back();</SCRIPT>";
}
/**
* 滤除帖子中的危险 HTML 代码, 主要是脚本代码, 滚动字幕代码以及脚本事件处理代码
*
* @param content
* 需要滤除的字符串
* @return 过滤的结果
*/
public static String replaceHtmlCode(String content) {
if (isEmpty(content)) {
return "";
}
// 需要滤除的脚本事件关键字
String[] eventKeywords = { "onmouseover", "onmouseout", "onmousedown",
"onmouseup", "onmousemove", "onclick", "ondblclick",
"onkeypress", "onkeydown", "onkeyup", "ondragstart",
"onerrorupdate", "onhelp", "onreadystatechange", "onrowenter",
"onrowexit", "onselectstart", "onload", "onunload",
"onbeforeunload", "onblur", "onerror", "onfocus", "onresize",
"onscroll", "oncontextmenu" };
content = replace(content, "<script", "<script", false);
content = replace(content, "</script", "</script", false);
content = replace(content, "<marquee", "<marquee", false);
content = replace(content, "</marquee", "</marquee", false);
content = replace(content, "\r\n", "<BR>");
// 滤除脚本事件代码
for (int i = 0; i < eventKeywords.length; i++) {
content = replace(content, eventKeywords[i],
"_" + eventKeywords[i], false); // 添加一个"_", 使事件代码无效
}
return content;
}
/**
* 滤除 HTML 代码 为文本代码.
*/
public static String replaceHtmlToText(String input) {
if (isEmpty(input)) {
return "";
}
return setBr(setTag(input));
}
/**
* 滤除 HTML 标记.
* 因为 XML 中转义字符依然有效, 因此把特殊字符过滤成中文的全角字符.
* @author beansoft
* @param s 输入的字串
* @return 过滤后的字串
*/
public static String setTag(String s) {
int j = s.length();
StringBuffer stringbuffer = new StringBuffer(j + 500);
char ch;
for (int i = 0; i < j; i++) {
ch = s.charAt(i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -