📄 stringutils.java
字号:
}
if (nameBuf.length() > 0)
{
String value =
(String) vars.get(nameBuf.toString());
if (value != null)
{
argBuf.append(value);
}
}
break;
default:
argBuf.append(ch);
++cIdx;
break;
}
}
return argBuf;
}
/**
* Read the contents of a file and place them in
* a string object.
*
* @param file path to file.
* @return String contents of the file.
*/
public static String fileContentsToString(String file)
{
String contents = "";
File f = null;
try
{
f = new File(file);
if (f.exists())
{
FileReader fr = null;
try
{
fr = new FileReader(f);
char[] template = new char[(int) f.length()];
fr.read(template);
contents = new String(template);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (fr != null)
{
fr.close();
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return contents;
}
/**
* Remove/collapse multiple newline characters.
*
* @param argStr string to collapse newlines in.
* @return String
*/
public static String collapseNewlines(String argStr)
{
char last = argStr.charAt(0);
StringBuffer argBuf = new StringBuffer();
for (int cIdx = 0 ; cIdx < argStr.length(); cIdx++)
{
char ch = argStr.charAt(cIdx);
if (ch != '\n' || last != '\n')
{
argBuf.append(ch);
last = ch;
}
}
return argBuf.toString();
}
/**
* Remove/collapse multiple spaces.
*
* @param argStr string to remove multiple spaces from.
* @return String
*/
public static String collapseSpaces(String argStr)
{
char last = argStr.charAt(0);
StringBuffer argBuf = new StringBuffer();
for (int cIdx = 0 ; cIdx < argStr.length(); cIdx++)
{
char ch = argStr.charAt(cIdx);
if (ch != ' ' || last != ' ')
{
argBuf.append(ch);
last = ch;
}
}
return argBuf.toString();
}
/**
* Replaces all instances of oldString with newString in line.
* Taken from the Jive forum package.
*
* @param line original string.
* @param oldString string in line to replace.
* @param newString replace oldString with this.
* @return String string with replacements.
*/
public static final String sub(String line, String oldString,
String newString)
{
int i = 0;
if ((i = line.indexOf(oldString, i)) >= 0)
{
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ((i = line.indexOf(oldString, i)) > 0)
{
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
/**
* Returns the output of printStackTrace as a String.
*
* @param e A Throwable.
* @return A String.
*/
public static final String stackTrace(Throwable e)
{
String foo = null;
try
{
// And show the Error Screen.
ByteArrayOutputStream ostr = new ByteArrayOutputStream();
e.printStackTrace( new PrintWriter(ostr,true) );
foo = ostr.toString();
}
catch (Exception f)
{
// Do nothing.
}
return foo;
}
/**
* Return a context-relative path, beginning with a "/", that represents
* the canonical version of the specified path after ".." and "." elements
* are resolved out. If the specified path attempts to go outside the
* boundaries of the current context (i.e. too many ".." path elements
* are present), return <code>null</code> instead.
*
* @param path Path to be normalized
* @return String normalized path
*/
public static final String normalizePath(String path)
{
// Normalize the slashes and add leading slash if necessary
String normalized = path;
if (normalized.indexOf('\\') >= 0)
{
normalized = normalized.replace('\\', '/');
}
if (!normalized.startsWith("/"))
{
normalized = "/" + normalized;
}
// Resolve occurrences of "//" in the normalized path
while (true)
{
int index = normalized.indexOf("//");
if (index < 0)
break;
normalized = normalized.substring(0, index) +
normalized.substring(index + 1);
}
// Resolve occurrences of "%20" in the normalized path
while (true)
{
int index = normalized.indexOf("%20");
if (index < 0)
break;
normalized = normalized.substring(0, index) + " " +
normalized.substring(index + 3);
}
// Resolve occurrences of "/./" in the normalized path
while (true)
{
int index = normalized.indexOf("/./");
if (index < 0)
break;
normalized = normalized.substring(0, index) +
normalized.substring(index + 2);
}
// Resolve occurrences of "/../" in the normalized path
while (true)
{
int index = normalized.indexOf("/../");
if (index < 0)
break;
if (index == 0)
return (null); // Trying to go outside our context
int index2 = normalized.lastIndexOf('/', index - 1);
normalized = normalized.substring(0, index2) +
normalized.substring(index + 3);
}
// Return the normalized path that we have completed
return (normalized);
}
/**
* If state is true then return the trueString, else
* return the falseString.
*
* @param state
* @param trueString
* @param falseString
* @return Selected result.
*/
public String select(boolean state, String trueString, String falseString)
{
if (state)
{
return trueString;
}
else
{
return falseString;
}
}
/**
* Check to see if all the string objects passed
* in are empty.
*
* @param list A list of {@link java.lang.String} objects.
* @return Whether all strings are empty.
*/
public boolean allEmpty(List list)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (list.get(i) != null && list.get(i).toString().length() > 0)
{
return false;
}
}
return true;
}
/**
* Trim all strings in a List. Changes the strings in the existing list.
* @param list
* @return List of trimmed strings.
*/
public static List trimStrings(List list)
{
if (list == null)
return null;
int sz = list.size();
for (int i = 0; i < sz; i++)
list.set(i,nullTrim((String) list.get(i)));
return list;
}
/**
* Trim the string, but pass a null through.
* @param s
* @return List of trimmed Strings.
*/
public static String nullTrim(String s)
{
if (s == null)
{
return null;
}
else
{
return s.trim();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -