📄 utilformatout.java
字号:
while (orgBuf.length() > 0 && orgBuf.charAt(0) == '0') {
orgBuf.deleteCharAt(0);
}
return orgBuf.toString();
}
// ------------------- date handlers -------------------
/** Formats a String timestamp into a nice string
* @param timestamp String timestamp to be formatted
* @return A String with the formatted date/time
*/
public static String formatDate(java.sql.Timestamp timestamp) {
if (timestamp == null)
return "";
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL);
java.util.Date date = (java.util.Date) timestamp;
return df.format(date);
}
// ------------------- null string handlers -------------------
/** Checks to see if the passed Object is null, if it is returns an empty but non-null string, otherwise calls toString() on the object
* @param obj1 The passed Object
* @return The toString() of the passed Object if not null, otherwise an empty non-null String
*/
public static String makeString(Object obj1) {
if (obj1 != null)
return obj1.toString();
else
return "";
}
/** Checks to see if the passed string is null, if it is returns an empty but non-null string.
* @param string1 The passed String
* @return The passed String if not null, otherwise an empty non-null String
*/
public static String checkNull(String string1) {
if (string1 != null)
return string1;
else
return "";
}
/** Returns the first passed String if not null, otherwise the second if not null, otherwise an empty but non-null String.
* @param string1 The first passed String
* @param string2 The second passed String
* @return The first passed String if not null, otherwise the second if not null, otherwise an empty but non-null String
*/
public static String checkNull(String string1, String string2) {
if (string1 != null)
return string1;
else if (string2 != null)
return string2;
else
return "";
}
/** Returns the first passed String if not null, otherwise the second if not null, otherwise the third if not null, otherwise an empty but non-null String.
* @param string1 The first passed String
* @param string2 The second passed String
* @param string3 The third passed String
* @return The first passed String if not null, otherwise the second if not null, otherwise the third if not null, otherwise an empty but non-null String
*/
public static String checkNull(String string1, String string2, String string3) {
if (string1 != null)
return string1;
else if (string2 != null)
return string2;
else if (string3 != null)
return string3;
else
return "";
}
/** Returns the first passed String if not null, otherwise the second if not null, otherwise the third if not null, otherwise the fourth if not null, otherwise an empty but non-null String.
* @param string1 The first passed String
* @param string2 The second passed String
* @param string3 The third passed String
* @param string4 The fourth passed String
* @return The first passed String if not null, otherwise the second if not null, otherwise the third if not null, otherwise the fourth if not null, otherwise an empty but non-null String
*/
public static String checkNull(String string1, String string2, String string3, String string4) {
if (string1 != null)
return string1;
else if (string2 != null)
return string2;
else if (string3 != null)
return string3;
else if (string4 != null)
return string4;
else
return "";
}
/** Returns <code>pre + base + post</code> if base String is not null or empty, otherwise an empty but non-null String.
* @param base The base String
* @param pre The pre String
* @param post The post String
* @return <code>pre + base + post</code> if base String is not null or empty, otherwise an empty but non-null String.
*/
public static String ifNotEmpty(String base, String pre, String post) {
if (base != null && base.length() > 0)
return pre + base + post;
else
return "";
}
/** Returns the first passed String if not empty, otherwise the second if not empty, otherwise an empty but non-null String.
* @param string1 The first passed String
* @param string2 The second passed String
* @return The first passed String if not empty, otherwise the second if not empty, otherwise an empty but non-null String
*/
public static String checkEmpty(String string1, String string2) {
if (string1 != null && string1.length() > 0)
return string1;
else if (string2 != null && string2.length() > 0)
return string2;
else
return "";
}
/** Returns the first passed String if not empty, otherwise the second if not empty, otherwise the third if not empty, otherwise an empty but non-null String.
* @param string1 The first passed String
* @param string2 The second passed String
* @param string3 The third passed String
* @return The first passed String if not empty, otherwise the second if not empty, otherwise the third if not empty, otherwise an empty but non-null String
*/
public static String checkEmpty(String string1, String string2, String string3) {
if (string1 != null && string1.length() > 0)
return string1;
else if (string2 != null && string2.length() > 0)
return string2;
else if (string3 != null && string3.length() > 0)
return string3;
else
return "";
}
// ------------------- web encode handlers -------------------
/** Encodes an HTTP URL query String, replacing characters used for other things in HTTP URL query strings, but not touching the separator characters '?', '=', and '&'
* @param query The plain query String
* @return The encoded String
*/
public static String encodeQuery(String query) {
String retString;
retString = replaceString(query, "%", "%25");
retString = replaceString(retString, " ", "%20");
return retString;
}
/** Encodes a single HTTP URL query value, replacing characters used for other things in HTTP URL query strings
* @param query The plain query value String
* @return The encoded String
*/
public static String encodeQueryValue(String query) {
String retString;
retString = replaceString(query, "%", "%25");
retString = replaceString(retString, " ", "%20");
retString = replaceString(retString, "&", "%26");
retString = replaceString(retString, "?", "%3F");
retString = replaceString(retString, "=", "%3D");
return retString;
}
/** Replaces all occurances of oldString in mainString with newString
* @param mainString The original string
* @param oldString The string to replace
* @param newString The string to insert in place of the old
* @return mainString with all occurances of oldString replaced by newString
*/
public static String replaceString(String mainString, String oldString, String newString) {
return StringUtil.replaceString(mainString, oldString, newString);
}
/** Decodes a single query value from an HTTP URL parameter, replacing %ASCII values with characters
* @param query The encoded query value String
* @return The plain, decoded String
*/
public static String decodeQueryValue(String query) {
String retString;
retString = replaceString(query, "%25", "%");
retString = replaceString(retString, "%20", " ");
retString = replaceString(retString, "%26", "&");
retString = replaceString(retString, "%3F", "?");
retString = replaceString(retString, "%3D", "=");
return retString;
}
// ------------------- web encode handlers -------------------
/** Encodes an XML string replacing the characters '<', '>', '"', ''', '&'
* @param inString The plain value String
* @return The encoded String
*/
public static String encodeXmlValue(String inString) {
String retString = inString;
retString = StringUtil.replaceString(retString, "&", "&");
retString = StringUtil.replaceString(retString, "<", "<");
retString = StringUtil.replaceString(retString, ">", ">");
retString = StringUtil.replaceString(retString, "\"", """);
retString = StringUtil.replaceString(retString, "'", "'");
return retString;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -