📄 stringutil.java
字号:
while (b <= ln) {
e = s.indexOf(c, b);
if (e == -1) e = ln;
res[i++] = s.substring(b, e);
b = e + 1;
}
return res;
}
/**
* Splits a string at the specified string.
*/
public static String[] split(String s, String sep, boolean caseInsensitive) {
String splitString = caseInsensitive ? sep.toLowerCase() : sep;
String input = caseInsensitive ? s.toLowerCase() : s;
int i, b, e;
int cnt;
String res[];
int ln = s.length();
int sln = sep.length();
if (sln == 0) throw new IllegalArgumentException(
"The separator string has 0 length");
i = 0;
cnt = 1;
while ((i = input.indexOf(splitString, i)) != -1) {
cnt++;
i += sln;
}
res = new String[cnt];
i = 0;
b = 0;
while (b <= ln) {
e = input.indexOf(splitString, b);
if (e == -1) e = ln;
res[i++] = s.substring(b, e);
b = e + sln;
}
return res;
}
/**
* Replaces all occurrences of a sub-string in a string.
* @param text The string where it will replace <code>oldsub</code> with
* <code>newsub</code>.
* @return String The string after the replacements.
*/
public static String replace(String text,
String oldsub,
String newsub,
boolean caseInsensitive,
boolean firstOnly)
{
StringBuffer buf;
int tln;
int oln = oldsub.length();
if (oln == 0) {
int nln = newsub.length();
if (nln == 0) {
return text;
} else {
if (firstOnly) {
return newsub + text;
} else {
tln = text.length();
buf = new StringBuffer(tln + (tln + 1) * nln);
buf.append(newsub);
for (int i = 0; i < tln; i++) {
buf.append(text.charAt(i));
buf.append(newsub);
}
return buf.toString();
}
}
} else {
oldsub = caseInsensitive ? oldsub.toLowerCase() : oldsub;
String input = caseInsensitive ? text.toLowerCase() : text;
int e = input.indexOf(oldsub);
if (e == -1) {
return text;
}
int b = 0;
tln = text.length();
buf = new StringBuffer(
tln + Math.max(newsub.length() - oln, 0) * 3);
do {
buf.append(text.substring(b, e));
buf.append(newsub);
b = e + oln;
e = input.indexOf(oldsub, b);
} while (e != -1 && !firstOnly);
buf.append(text.substring(b));
return buf.toString();
}
}
/**
* Removes the line-break from the end of the string.
*/
public static String chomp(String s) {
if (s.endsWith("\r\n")) return s.substring(0, s.length() - 2);
if (s.endsWith("\r") || s.endsWith("\n"))
return s.substring(0, s.length() - 1);
return s;
}
/**
* Quotes string as Java Language string literal.
* Returns string <code>"null"</code> if <code>s</code>
* is <code>null</code>.
*/
public static String jQuote(String s) {
if (s == null) {
return "null";
}
int ln = s.length();
StringBuffer b = new StringBuffer(ln + 4);
b.append('"');
for (int i = 0; i < ln; i++) {
char c = s.charAt(i);
if (c == '"') {
b.append("\\\"");
} else if (c == '\\') {
b.append("\\\\");
} else if (c < 0x20) {
if (c == '\n') {
b.append("\\n");
} else if (c == '\r') {
b.append("\\r");
} else if (c == '\f') {
b.append("\\f");
} else if (c == '\b') {
b.append("\\b");
} else if (c == '\t') {
b.append("\\t");
} else {
b.append("\\u00");
int x = c / 0x10;
b.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
x = c & 0xF;
b.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
}
} else {
b.append(c);
}
} // for each characters
b.append('"');
return b.toString();
}
/**
* Escapes the <code>String</code> with the escaping rules of Java language
* string literals, so it is safe to insert the value into a string literal.
* The resulting string will not be quoted.
*
* <p>In additional, all characters under UCS code point 0x20, that has no
* dedicated escape sequence in Java language, will be replaced with UNICODE
* escape (<tt>\<!-- -->u<i>XXXX</i></tt>).
*
* @see #jQuote(String)
*/
public static String javaStringEnc(String s) {
int ln = s.length();
for (int i = 0; i < ln; i++) {
char c = s.charAt(i);
if (c == '"' || c == '\\' || c < 0x20) {
StringBuffer b = new StringBuffer(ln + 4);
b.append(s.substring(0, i));
while (true) {
if (c == '"') {
b.append("\\\"");
} else if (c == '\\') {
b.append("\\\\");
} else if (c < 0x20) {
if (c == '\n') {
b.append("\\n");
} else if (c == '\r') {
b.append("\\r");
} else if (c == '\f') {
b.append("\\f");
} else if (c == '\b') {
b.append("\\b");
} else if (c == '\t') {
b.append("\\t");
} else {
b.append("\\u00");
int x = c / 0x10;
b.append((char)
(x < 0xA ? x + '0' : x - 0xA + 'a'));
x = c & 0xF;
b.append((char)
(x < 0xA ? x + '0' : x - 0xA + 'a'));
}
} else {
b.append(c);
}
i++;
if (i >= ln) {
return b.toString();
}
c = s.charAt(i);
}
} // if has to be escaped
} // for each characters
return s;
}
/**
* Escapes a <code>String</code> according the JavaScript string literal
* escaping rules. The resulting string will not be quoted.
*
* <p>It escapes both <tt>'</tt> and <tt>"</tt>.
* In additional it escapes <tt>></tt> as <tt>\></tt> (to avoid
* <tt></script></tt>). Furthermore, all characters under UCS code point
* 0x20, that has no dedicated escape sequence in JavaScript language, will
* be replaced with hexadecimal escape (<tt>\x<i>XX</i></tt>).
*/
public static String javaScriptStringEnc(String s) {
int ln = s.length();
for (int i = 0; i < ln; i++) {
char c = s.charAt(i);
if (c == '"' || c == '\'' || c == '\\' || c == '>' || c < 0x20) {
StringBuffer b = new StringBuffer(ln + 4);
b.append(s.substring(0, i));
while (true) {
if (c == '"') {
b.append("\\\"");
} else if (c == '\'') {
b.append("\\'");
} else if (c == '\\') {
b.append("\\\\");
} else if (c == '>') {
b.append("\\>");
} else if (c < 0x20) {
if (c == '\n') {
b.append("\\n");
} else if (c == '\r') {
b.append("\\r");
} else if (c == '\f') {
b.append("\\f");
} else if (c == '\b') {
b.append("\\b");
} else if (c == '\t') {
b.append("\\t");
} else {
b.append("\\x");
int x = c / 0x10;
b.append((char)
(x < 0xA ? x + '0' : x - 0xA + 'A'));
x = c & 0xF;
b.append((char)
(x < 0xA ? x + '0' : x - 0xA + 'A'));
}
} else {
b.append(c);
}
i++;
if (i >= ln) {
return b.toString();
}
c = s.charAt(i);
}
} // if has to be escaped
} // for each characters
return s;
}
/**
* Parses a name-value pair list, where the pairs are separated with comma,
* and the name and value is separated with colon.
* The keys and values can contain only letters, digits and <tt>_</tt>. They
* can't be quoted. White-space around the keys and values are ignored. The
* value can be omitted if <code>defaultValue</code> is not null. When a
* value is omitted, then the colon after the key must be omitted as well.
* The same key can't be used for multiple times.
*
* @param s the string to parse.
* For example: <code>"strong:100, soft:900"</code>.
* @param defaultValue the value used when the value is omitted in a
* key-value pair.
*
* @return the map that contains the name-value pairs.
*
* @throws java.text.ParseException if the string is not a valid name-value
* pair list.
*/
public static Map parseNameValuePairList(String s, String defaultValue)
throws java.text.ParseException {
Map map = new HashMap();
char c = ' ';
int ln = s.length();
int p = 0;
int keyStart;
int valueStart;
String key;
String value;
fetchLoop: while (true) {
// skip ws
while (p < ln) {
c = s.charAt(p);
if (!Character.isWhitespace(c)) {
break;
}
p++;
}
if (p == ln) {
break fetchLoop;
}
keyStart = p;
// seek key end
while (p < ln) {
c = s.charAt(p);
if (!(Character.isLetterOrDigit(c) || c == '_')) {
break;
}
p++;
}
if (keyStart == p) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -