📄 stringutil.java
字号:
* @param j
* @return
*/
public static String doNull(int i, String j) {
Integer intrel = new Integer(i);
if (intrel == null || (intrel.intValue() == 0)) {
return j;
} else {
return intrel.toString();
}
}
/**
* if i != NULL or i != 0, return i, otherwise return j.
*
* @param i
* @param j
* @return
*/
public static String doNull(double i, String j) {
Double rel = new Double(i);
if ((rel == null) || (rel.doubleValue() == 0)) {
return j;
} else {
return rel.toString();
}
}
/**
* if i != NULL or i != 0, return i, otherwise return j.
*
* @param i
* @param j
* @return
*/
public static String doNull(long i, String j) {
Long rel = new Long(i);
if (rel == null || (rel.longValue() == 0)) {
return j;
} else {
return rel.toString();
}
}
/**
* Get String as specified Encoding.
*
* @param s
* @param strEnCoding
* @return
*/
public static String getString(String s, String strEnCoding) {
String rel = "";
if (s == null) {
return "";
} else {
try {
rel = new String(s.getBytes(strEnCoding));
} catch (Exception e) {
System.out.println(e);
}
return rel;
}
}
/**
* deal with single quotes
*
* @param str
* @return
*/
public static String strStandard(String str) {
String rel = "";
int intcurrent = 0;
int intlast = 0;
while (intcurrent != -1) {
intcurrent = str.indexOf("'", intcurrent);
if (intcurrent != -1)
rel = rel + str.substring(intlast, intcurrent) + "'";
else
break;
intlast = intcurrent;
intcurrent++;
}// end of while
return rel + str.substring(intlast);
}// end of strStandard
/**
* write String to a file
*
* @param str
* @param strFileName
* @return
*/
public static boolean string2File(String str, String strFileName) {
byte[] blist = str.getBytes();
try {
OutputStream os = new FileOutputStream(strFileName);
os.write(blist);
os.close();
} catch (IOException e) {
System.out.print(e);
return false;
}
return true;
}
/**
* return the same string that two string have
*
* @param strSource
* @param str2
* @return
*/
public static String findSameChar(String strSource, String str2) {
int i;
String TempStr, SearchStr;
if ((strSource.length() == 0) || (str2.length() == 0)) {
return "";
}
TempStr = "";
for (i = 0; i < strSource.length(); i++) {
SearchStr = strSource.substring(i, i + 1);
if (str2.indexOf(SearchStr) != -1)
TempStr = TempStr + SearchStr;
}
return TempStr;
}// end of FindSameChar
/**
* process chinese characters.
*
* @param str
* @param intStart
* start from 1.
* @param intLength
* @return
*/
public static String truncate(String str, int intStart, int intLength) {
String retVal, strTmp;
int i, intRealLength, intUnicodeLength;
boolean boolFindChinese = false;
intUnicodeLength = str.length();
intRealLength = 0;
retVal = "";
for (i = 1; i <= intUnicodeLength; i++) {
boolFindChinese = false;
strTmp = str.substring(i - 1, i);
if (strTmp.getBytes().length > 1) {
intRealLength += 1;
boolFindChinese = true;
if ((intRealLength >= intStart) && ((intRealLength + 1) <= (intStart + intLength - 1)))
retVal = retVal + strTmp;
else if ((intRealLength < intStart) && ((intRealLength + 1) == intStart))
retVal = retVal + "?";
else if (intRealLength == (intStart + intLength - 1))
retVal = retVal + "?";
intRealLength += 1;
}// end if it is Chinese Character.
if (!boolFindChinese) {
intRealLength += 1;
if ((intRealLength >= intStart) && ((intRealLength) <= (intStart + intLength - 1)))
retVal = retVal + strTmp;
}// end if it is not Chinese Character.
}// end for(i)
if (retVal != null)
return retVal;
else
return "";
}// end Truncate
/**
* String s="this is {0} and {1}"; StringUtil.format(s,new String[]{"a","b"}); return "this is a and b";
*
* @param source
* @param args
* @return
*/
public static String format(String source, String[] args) {
MessageFormat format = new MessageFormat(source);
return format.format(args);
}
/**
* @see public static String format(String source, String[] args)
* @param source
* @param arg1
* @return
*/
public static String format(String source, String arg1) {
return format(source, new String[] { arg1 });
}
/**
* @see public static String format(String source, String[] args)
* @param source
* @param arg1
* @param arg2
* @return
*/
public static String format(String source, String arg1, String arg2) {
return format(source, new String[] { arg1, arg2 });
}
/**
* @see public static String format(String source, String[] args)
* @param source
* @param arg1
* @param arg2
* @param arg3
* @return
*/
public static String format(String source, String arg1, String arg2, String arg3) {
return format(source, new String[] { arg1, arg2, arg3 });
}
/**
* @see public static String format(String source, String[] args)
* @param source
* @param arg1
* @param arg2
* @param arg3
* @param arg4
* @return
*/
public static String format(String source, String arg1, String arg2, String arg3, String arg4) {
return format(source, new String[] { arg1, arg2, arg3, arg4 });
}
/**
* @see public static String format(String source, String[] args)
* @param source
* @param arg1
* @param arg2
* @param arg3
* @param arg4
* @param arg5
* @return
*/
public static String format(String source, String arg1, String arg2, String arg3, String arg4, String arg5) {
return format(source, new String[] { arg1, arg2, arg3, arg4, arg5 });
}
public static String getText(Object data) {
if (isEmpty(data))
return "";
else
return data.toString();
}
public static String getText(String data) {
if (isEmpty(data))
return "";
else
return data;
}
public static String getText(Long data) {
if (isEmpty(data))
return "";
else
return data.toString();
}
public static String getText(Integer data) {
if (isEmpty(data))
return "";
else
return data.toString();
}
public static String getText(Double data) {
if (isEmpty(data))
return "";
else
return data.toString();
}
public static String getText(Float data) {
if (isEmpty(data))
return "";
else
return data.toString();
}
public static String getText(java.sql.Date data) {
if (isEmpty(data))
return "";
else
return DateUtils.format(data);
}
public static boolean isEmpty(Object obj) {
if(obj instanceof String) {
return obj == null || obj.equals("");
}
return obj == null;
}
public static boolean isEmpty(Set set) {
return set == null || set.isEmpty();
}
public static boolean isEmpty(Map map) {
return map == null || map.isEmpty();
}
public static boolean isEmpty(Long data) {
return data == null;
}
public static boolean isEmpty(Integer data) {
return data == null;
}
public static boolean isEmpty(Double data) {
return data == null;
}
public static boolean isEmpty(Float data) {
return data == null;
}
public static boolean isEmpty(Date data) {
return data == null;
}
public static boolean isEmpty(java.sql.Date data) {
return data == null;
}
public static boolean isEmpty(Time data) {
return data == null;
}
public static boolean isEmpty(Timestamp data) {
return data == null;
}
public static boolean isEmpty(Collection collection) {
return collection == null || collection.isEmpty();
}
public static boolean isEmpty(Object array[]) {
return array == null || array.length == 0;
}
public static String ListToString(List<String> list) {
String result = "";
if(list != null && !list.isEmpty()) {
for(String message : list) {
result += message + " , ";
}
}
if(result.length() > 0) {
result = result.substring(0,result.length() - 1);
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -