📄 stringutils.java
字号:
return values;
}
/**
* Joins the elements of the provided array into a single string
* containing a list of CSV elements.
*
* @param list The list of values to join together.
* @param separator The separator character.
* @return The CSV text.
*/
public static String join(String[] list, String separator) {
StringBuffer csv = new StringBuffer();
for (int i = 0; i < list.length; i++) {
if (i > 0) {
csv.append(separator);
}
csv.append(list[i]);
}
return csv.toString();
}
/**
* Takes a block of text which might have long lines in it and wraps
* the long lines based on the supplied wrapColumn parameter. It was
* initially implemented for use by VelocityEmail. If there are tabs
* in inString, you are going to get results that are a bit strange,
* since tabs are a single character but are displayed as 4 or 8
* spaces. Remove the tabs.
*
* @param inString Text which is in need of word-wrapping.
* @param newline The characters that define a newline.
* @param wrapColumn The column to wrap the words at.
* @return The text with all the long lines word-wrapped.
*/
public static String wrapText(String inString, String newline,
int wrapColumn) {
StringTokenizer lineTokenizer = new StringTokenizer(inString, newline,
true);
StringBuffer stringBuffer = new StringBuffer();
while (lineTokenizer.hasMoreTokens()) {
try {
String nextLine = lineTokenizer.nextToken();
if (nextLine.length() > wrapColumn) {
// This line is long enough to be wrapped.
nextLine = wrapLine(nextLine, newline, wrapColumn);
}
stringBuffer.append(nextLine);
} catch (NoSuchElementException nsee) {
// thrown by nextToken(), but I don't know why it would
break;
}
}
return (stringBuffer.toString());
}
/**
* Wraps a single line of text. Called by wrapText(). I can't
* think of any good reason for exposing this to the public,
* since wrapText should always be used AFAIK.
*
* @param line A line which is in need of word-wrapping.
* @param newline The characters that define a newline.
* @param wrapColumn The column to wrap the words at.
* @return A line with newlines inserted.
*/
protected static String wrapLine(String line, String newline, int wrapColumn) {
StringBuffer wrappedLine = new StringBuffer();
while (line.length() > wrapColumn) {
int spaceToWrapAt = line.lastIndexOf(' ', wrapColumn);
if (spaceToWrapAt >= 0) {
wrappedLine.append(line.substring(0, spaceToWrapAt));
wrappedLine.append(newline);
line = line.substring(spaceToWrapAt + 1);
}
// This must be a really long word or URL. Pass it
// through unchanged even though it's longer than the
// wrapColumn would allow. This behavior could be
// dependent on a parameter for those situations when
// someone wants long words broken at line length.
else {
spaceToWrapAt = line.indexOf(' ', wrapColumn);
if (spaceToWrapAt >= 0) {
wrappedLine.append(line.substring(0, spaceToWrapAt));
wrappedLine.append(newline);
line = line.substring(spaceToWrapAt + 1);
} else {
wrappedLine.append(line);
line = "";
}
}
}
// Whatever is left in line is short enough to just pass through,
// just like a small small kidney stone
wrappedLine.append(line);
return wrappedLine.toString();
}
/**
* convert the ISO char encoding to GBK
* @param str the ISO encoding string
* @return the GBK encoding string
* created by yanfeng at 13/5/2003
* modified by yanfeng at14/7/2003
* for recursive invoke of this function in log.error()
* @CheckItem@ SELFBUG-yanfeng-20030714 可能产生循环调用
*/
public static String ISOtoGBK(String str) {
if (str == null) {
return null;
}
byte[] by = null;
try {
by = str.getBytes("ISO8859_1");
} catch (UnsupportedEncodingException ex) {
return str;
}
try {
String a = new String(by, "GBK");
return a;
} catch (UnsupportedEncodingException ex1) {
return str;
}
}
/**
* 根据系统环境自动判断是否转码
* @param str String
* @return String
*/
public static String GBKtoISOByEncode(String str) {
if (str == null) {
return null;
}
if (isISO) {//是
byte[] by = null;
try {
by = str.getBytes("GBK");
} catch (UnsupportedEncodingException ex) {
return str;
}
try {
String a = new String(by, "ISO8859_1");
return a;
} catch (UnsupportedEncodingException ex1) {
return str;
}
} else {
return str;
}
}
public static String GBKtoISO(String str) {
if (str == null) {
return null;
}
byte[] by = null;
try {
by = str.getBytes("GBK");
} catch (UnsupportedEncodingException ex) {
return str;
}
try {
String a = new String(by, "ISO8859_1");
return a;
} catch (UnsupportedEncodingException ex1) {
return str;
}
}
/**
* trim the string even when it's null
* @param str the string need to be trimmed
* @return the trimmed string
*/
public static String trim(String str) {
if (str == null) {
return "";
}
return str.trim();
}
/**
* 判断str是否在strArr中
* @param str
* @param strArr
* @return true:str在strArr中出现,;false otherwise.
*/
public static boolean stringInArray(String str, String[] strArr) {
for (int i = 0; i < strArr.length; i++) {
if (str.equals(strArr[i])) {
return true;
}
}
return false;
}
/**
* 将带有格式的字符串转换成HTML方式
* @param origine
* @return
*/
public static String convert2Html(String origine) {
String outStr = null;
if (origine != null) {
String tmp = StringUtils.replace(origine, ">", ">");
String tmp2 = StringUtils.replace(tmp, "<", "<");
String tmp3 = StringUtils.replace(tmp2, " ", " ");
String tmp4 = StringUtils.replace(tmp3, "\r\n", "<br>");
outStr = tmp4;
} else {
outStr = "";
}
return outStr;
}
public static void main(String[] args) {
String a = "?";
System.out.println(replace(a, "", "oo"));
}
/**
* 找出字符串在数组中的位置(序号),若不存在,返回数组的长度
* @param myStr 字符串
* @param myList 字符串数组
* @return int
* @author Zhang Zhongguang
*/
public static int getStringsIndex(String myStr, String[] myList) {
int i = 0;
for (i = 0; i < myList.length; i++) {
if (myList[i].equals(myStr)) {
break;
}
}
return i;
}
/**
* 将字符串src按format指定的格式转化为日期对象,本地中文
* @param src String
* @param format String 格式,如yy/MM/dd hh:mm:ss
* @return Date对象
* @throws ParseException
* @author Zhang Zhongguang
*/
public static Date toDate(String src, String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format,
java.util.Locale.CHINESE);
Date d = null;
d = sdf.parse(src);
return d;
}
/**
* 去src数组中为null的元素,返回新的数组
* @param src
* @return String[]
* @author Zhang zhongguang
*/
public static String[] trimNull(String[] src) {
if (src == null) {
return null;
}
String[] tmp = null;
int count = 0;
//取null元素数目
for (int i = 0; i < src.length; i++) {
if (src[i] == null) {
count++;
}
}
//剔除
if (count == 0) {
tmp = src;
} else if (count < src.length) {
tmp = new String[src.length - count];
int j = 0;
for (int i = 0; i < src.length; i++) {
if (src[i] != null) {
tmp[j++] = src[i];
}
}
}
return tmp;
}
public static String convertToISO(String str) {
String sRetVal = "";
if (str == null) {
sRetVal = "";
} else {
try {
sRetVal = new String(str.trim().getBytes("ISO8859_1"), "GBK");
} catch (UnsupportedEncodingException ex) {
sRetVal = "";
}
}
return sRetVal;
}
public static String convertToISO2(String str) {
String sRetVal = "";
if (str == null) {
sRetVal = "";
} else {
try {
sRetVal = new String(str.getBytes("ISO8859_1"), "GBK");
} catch (UnsupportedEncodingException ex) {
sRetVal = "";
}
}
return sRetVal;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -