📄 utils.java
字号:
}
else {
buf.append((char)(iso.intValue()));
}
i = semi;
}
else {
buf.append(ch);
}
}
return buf.toString();
}
/**
* Prepares a string for output inside a JavaScript string,
* e.g. for use inside a document.write("") command.
*
* Example:
* <pre>
* input string: He didn't say, "Stop!"
* output string: He didn\'t say, \"Stop!\"
* </pre>
*
* Deals with quotes and control-chars (tab, backslash, cr, ff, etc.)
* Bug: does not yet properly escape Unicode / high-bit characters.
*
* @see #jsEscape(String, Writer)
**/
public static String jsEscape(String source) {
try {
StringWriter sw = new StringWriter();
jsEscape(source, sw);
sw.flush();
return sw.toString();
}
catch (IOException ioe) {
// should never happen writing to a StringWriter
ioe.printStackTrace();
return null;
}
}
/**
* @see #javaEscape(String, Writer)
**/
public static String javaEscape(String source) {
try {
StringWriter sw = new StringWriter();
javaEscape(source, sw);
sw.flush();
return sw.toString();
}
catch (IOException ioe) {
// should never happen writing to a StringWriter
ioe.printStackTrace();
return null;
}
}
public static String readLine(InputStream in) throws IOException, EOFException {
StringBuffer buf = new StringBuffer();
int ch;
while((ch = in.read()) > -1) {
if(ch=='\n')
break;
buf.append((char)ch);
}
if(ch != '\n' && buf.length()==0)
return null;
return buf.toString();
}
/**
* Prepares a string for output inside a JavaScript string,
* e.g. for use inside a document.write("") command.
*
* Example:
* <pre>
* input string: He didn't say, "stop!"
* output string: He didn\'t say, \"stop!\"
* </pre>
*
* Deals with quotes and control-chars (tab, backslash, cr, ff, etc.)
*
* @see #jsEscape(String)
**/
public static void jsEscape(String source, Writer out) throws IOException {
stringEscape(source, out, true);
}
/**
* Prepares a string for output inside a Java string,
*
* Example:
* <pre>
* input string: He didn't say, "stop!"
* output string: He didn't say, \"stop!\"
* </pre>
*
* Deals with quotes and control-chars (tab, backslash, cr, ff, etc.)
*
* @see #jsEscape(String,Writer)
**/
public static void javaEscape(String source, Writer out) throws IOException {
stringEscape(source, out, false);
}
private static void stringEscape(String source, Writer out, boolean escapeSingleQuote) throws IOException {
char[] chars = source.toCharArray();
for (int i=0; i<chars.length; ++i) {
char ch = chars[i];
switch (ch) {
case '\b': // backspace (ASCII 8)
out.write("\\b");
break;
case '\t': // horizontal tab (ASCII 9)
out.write("\\t");
break;
case '\n': // newline (ASCII 10)
out.write("\\n");
break;
case 11: // vertical tab (ASCII 11)
out.write("\\v");
break;
case '\f': // form feed (ASCII 12)
out.write("\\f");
break;
case '\r': // carriage return (ASCII 13)
out.write("\\r");
break;
case '"': // double-quote (ASCII 34)
out.write("\\\"");
break;
case '\'': // single-quote (ASCII 39)
if (escapeSingleQuote) out.write("\\'");
else out.write("'");
break;
case '\\': // literal backslash (ASCII 92)
out.write("\\\\");
break;
default:
if ((int) ch < 32 || (int) ch > 127)
{
out.write("\\u");
zeropad(out, Integer.toHexString(ch).toUpperCase(), 4);
}
else
{
out.write(ch);
}
break;
}
}
}
private static void zeropad(Writer out, String s, int width) throws IOException
{
while (width > s.length()) {
out.write('0');
width--;
}
out.write(s);
}
/**
* Filter out Windows and Mac curly quotes, replacing them with
* the non-curly versions. Note that this doesn't actually do any
* checking to verify the input codepage. Instead it just
* converts the more common code points used on the two platforms
* to their equivalent ASCII values. As such, this method
* <B>should not be used</b> on ISO-8859-1 input that includes
* high-bit-set characters, and some text which uses other
* codepoints may be rendered incorrectly.
*
* @author Ian McFarland
**/
public static String uncurlQuotes(String input)
{
if (input==null)
return "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < input.length(); i++)
{
char ch = input.charAt(i);
int code = (int) ch;
if (code == 210 || code == 211 || code == 147 || code == 148)
{
ch = (char) 34; // double quote
}
else if (code == 212 || code == 213 || code == 145 || code == 146)
{
ch = (char) 39; // single quote
}
sb.append(ch);
}
return sb.toString();
}
/**
* capitalize the first character of s
**/
public static String capitalize(String s) {
return s.substring(0,1).toUpperCase() + s.substring(1);
}
/**
* lowercase the first character of s
**/
public static String lowerize(String s) {
return s.substring(0,1).toLowerCase() + s.substring(1);
}
/**
* turn String s into a plural noun (doing the right thing with
* "story" -> "stories" and "mess" -> "messes")
**/
public static String pluralize(String s) {
if (s.endsWith("y"))
return s.substring(0, s.length()-1) + "ies";
else if (s.endsWith("s"))
return s + "es";
else
return s + "s";
}
public static boolean ok(String s) {
return (!(s == null || s.equals("")));
}
/**
* Converts camelCaseVersusC to camel_case_versus_c
**/
public static String toUnderscore(String s) {
StringBuffer buf = new StringBuffer();
char[] ch = s.toCharArray();
for (int i=0; i<ch.length; ++i) {
if (Character.isUpperCase(ch[i])) {
buf.append('_');
buf.append(Character.toLowerCase(ch[i]));
}
else {
buf.append(ch[i]);
}
}
//System.err.println(s + " -> " + buf.toString());
return buf.toString();
}
/**
* @deprecated use org.apache.commons.lang.StringUtils deleteSpaces instead
**/
public static String stripWhitespace(String s) {
StringBuffer buf = new StringBuffer();
char[] ch = s.toCharArray();
for (int i=0; i<ch.length; ++i) {
if (Character.isWhitespace(ch[i])) {
continue;
}
else {
buf.append(ch[i]);
}
}
return buf.toString();
}
public static String getStackTrace(Throwable t) {
StringWriter s = new StringWriter();
PrintWriter p = new PrintWriter(s);
t.printStackTrace(p);
p.close();
return s.toString();
}
public static void sleep(long msec) {
try {
Thread.sleep(msec);
}
catch (InterruptedException ie) {}
}
} // class Utils
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -