📄 stringutils.java
字号:
if (c == separatorChar) {
String e = buff.toString();
list.add(trim ? e.trim() : e);
buff.setLength(0);
} else if (c == '\\' && i < s.length() - 1) {
buff.append(s.charAt(++i));
} else {
buff.append(c);
}
}
String e = buff.toString();
list.add(trim ? e.trim() : e);
String[] array = new String[list.size()];
list.toArray(array);
return array;
}
public static String arrayCombine(String[] list, char separatorChar) {
StringBuffer buff = new StringBuffer(5 * list.length);
for (int i = 0; i < list.length; i++) {
if (i > 0) {
buff.append(separatorChar);
}
String s = list[i];
if (s == null) {
s = "";
}
for (int j = 0; j < s.length(); j++) {
char c = s.charAt(j);
if (c == '\\' || c == separatorChar) {
buff.append('\\');
}
buff.append(c);
}
}
return buff.toString();
}
/**
* Formats a date using a format string
*/
public static String formatDateTime(Date date, String format, String locale, String timezone) throws SQLException {
SimpleDateFormat dateFormat = getDateFormat(format, locale, timezone);
synchronized (dateFormat) {
return dateFormat.format(date);
}
}
/**
* Parses a date using a format string
*/
public static Date parseDateTime(String date, String format, String locale, String timezone) throws SQLException {
SimpleDateFormat dateFormat = getDateFormat(format, locale, timezone);
try {
synchronized (dateFormat) {
return dateFormat.parse(date);
}
} catch (ParseException e) {
throw Message.getSQLException(ErrorCode.PARSE_ERROR_1, new String[]{date}, e);
}
}
private static SimpleDateFormat getDateFormat(String format, String locale, String timezone) throws SQLException {
try {
// currently, a new instance is create for each call
// however, could cache the last few instances
SimpleDateFormat df;
if (locale == null) {
df = new SimpleDateFormat(format);
} else {
//#ifdef JDK14
Locale l = new Locale(locale);
//#endif
//#ifdef JDK13
/*
Locale l = new Locale(locale, "");
*/
//#endif
df = new SimpleDateFormat(format, l);
}
if (timezone != null) {
df.setTimeZone(TimeZone.getTimeZone(timezone));
}
return df;
} catch (Exception e) {
throw Message.getSQLException(ErrorCode.PARSE_ERROR_1, new String []{format + "/" + locale + "/" + timezone}, e);
}
}
/**
* Creates an XML attribute of the form name="value".
* A single space is prepended to the name,
* so that multiple attributes can be concatenated.
* @param name
* @param value
* @return the attribute
*/
public static String xmlAttr(String name, String value) {
return " " + name + "=\"" + xmlText(value) + "\"";
}
/**
* Create an XML node with optional attributes and content.
* The data is indented with 4 spaces if it contains a newline character.
*
* @param name the element name
* @param attributes the attributes (may be null)
* @param content the content (may be null)
* @return the node
*/
public static String xmlNode(String name, String attributes, String content) {
String start = attributes == null ? name : name + attributes;
if (content == null) {
return "<" + start + "/>\n";
} else {
if (content.indexOf('\n') >= 0) {
content = "\n" + indent(content);
}
return "<" + start + ">" + content + "</" + name + ">\n";
}
}
/**
* Indents a string with 4 spaces.
* @param s the string
* @return the indented string
*/
public static String indent(String s) {
return indent(s, 4);
}
/**
* Indents a string with spaces.
* @param s the string
* @param spaces the number of spaces
* @return the indented string
*/
public static String indent(String s, int spaces) {
StringBuffer buff = new StringBuffer(s.length() + spaces);
for (int i = 0; i < s.length();) {
for (int j = 0; j < spaces; j++) {
buff.append(' ');
}
int n = s.indexOf('\n', i);
n = n < 0 ? s.length() : n + 1;
buff.append(s.substring(i, n));
i = n;
}
if (!s.endsWith("\n")) {
buff.append('\n');
}
return buff.toString();
}
/**
* Escapes a comment.
* If the data contains '--', it is converted to '- -'.
* The data is indented with 4 spaces if it contains a newline character.
*
* @param data
* @return <!-- data -->
*/
public static String xmlComment(String data) {
int idx = 0;
while (true) {
idx = data.indexOf("--", idx);
if (idx < 0) {
break;
}
data = data.substring(0, idx + 1) + " " + data.substring(idx + 1);
}
// must have a space at the beginning and at the end,
// otherwise the data must not contain '-' as the first/last character
if (data.indexOf('\n') >= 0) {
return "<!--\n" + indent(data) + "-->\n";
} else {
return "<!-- " + data + " -->\n";
}
}
/**
* Converts the data to a CDATA element.
* If the data contains ']]>', it is escaped as a text element.
* @param data
* @return <![CDATA[data]]>
*/
public static String xmlCData(String data) {
if (data.indexOf("]]>") >= 0) {
return xmlText(data);
}
boolean newline = data.endsWith("\n");
data = "<![CDATA[" + data + "]]>";
return newline ? data + "\n" : data;
}
/**
* Returns <?xml version="1.0"?>
* @return <?xml version="1.0"?>
*/
public static String xmlStartDoc() {
return "<?xml version=\"1.0\"?>\n";
}
/**
* Escapes an XML text element.
*
* @param text
* @return the escaped text
*/
public static String xmlText(String text) {
StringBuffer buff = new StringBuffer(text.length());
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
switch (ch) {
case '<':
buff.append("<");
break;
case '>':
buff.append(">");
break;
case '&':
buff.append("&");
break;
case '\'':
buff.append("'");
break;
case '\"':
buff.append(""");
break;
case '\r':
case '\n':
case '\t':
buff.append(ch);
break;
default:
if (ch < ' ' || ch > 127) {
buff.append("&#x");
buff.append(Integer.toHexString(ch));
buff.append(';');
} else {
buff.append(ch);
}
}
}
return buff.toString();
}
public static String replaceAll(String s, String before, String after) {
int index = 0;
while (true) {
int next = s.indexOf(before, index);
if (next < 0) {
return s;
}
s = s.substring(0, next) + after + s.substring(next + before.length());
index = next + after.length();
}
}
public static String quoteIdentifier(String s) {
StringBuffer buff = new StringBuffer(s.length() + 2);
buff.append('\"');
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '"') {
buff.append(c);
}
buff.append(c);
}
return buff.append('\"').toString();
}
/**
* Check if a String is null or empty (the length is null).
*
* @return true if it is null or empty
*/
public static boolean isNullOrEmpty(String s) {
return s == null || s.length() == 0;
}
public static String quoteRemarkSQL(String sql) {
while (true) {
int idx = sql.indexOf("*/");
if (idx < 0) {
break;
}
sql = sql.substring(0, idx) + "++/" + sql.substring(idx + 2);
}
while (true) {
int idx = sql.indexOf("/*");
if (idx < 0) {
break;
}
sql = sql.substring(0, idx) + "/++" + sql.substring(idx + 2);
}
return sql;
}
/**
* Pad a string. This method is used for the SQL function RPAD and LPAD.
*
* @param string the original string
* @param n the target length
* @param padding the padding string
* @param right true if the padding should be appended at the end
* @return the padded string
*/
public static String pad(String string, int n, String padding, boolean right) {
if (n < 0) {
n = 0;
}
if (n < string.length()) {
return string.substring(0, n);
} else if (n == string.length()) {
return string;
}
char paddingChar;
if (padding == null || padding.length() == 0) {
paddingChar = ' ';
} else {
paddingChar = padding.charAt(0);
}
StringBuffer buff = new StringBuffer(n);
n -= string.length();
if (right) {
buff.append(string);
}
for (int i = 0; i < n; i++) {
buff.append(paddingChar);
}
if (!right) {
buff.append(string);
}
return buff.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -