1.String類:String.format()
這貨有2種重載形式:
format(String format, Object… args)
新字元串使用本地語言環境,制定字元串格式和參數生成格式化的新字元串。
format(Locale locale, String format, Object… args)
使用指定的語言環境,制定字元串格式和參數生成格式化的字元串。
轉換符 | 詳細說明 | 示例 |
%s | 字元串類型 | “喜歡請收藏” |
%c | 字元類型 | ‘m’ |
%b | 布爾類型 | true |
%d | 整數類型(十進制) | 88 |
%x | 整數類型(十六進制) | FF |
%o | 整數類型(八進制) | 77 |
%f | 浮點類型 | 8.888 |
%a | 十六進制浮點類型 | FF.35AE |
%e | 指數類型 | 9.38e+5 |
%g | 通用浮點類型(f和e類型中較短的) | 不舉例(基本用不到) |
%h | 散列碼 | 不舉例(基本用不到) |
%% | 百分比類型 | %(%特殊字元%%才能顯示%) |
%n | 換行符 | 不舉例(基本用不到) |
%tx | 日期與時間類型(x代表不同的日期與時間轉換符) | 不舉例(基本用不到) |
StringUtils.chomp()
public static String chomp(String str)
{
if (isEmpty(str)) {
return str;
}
if (str.length() == 1) {
char ch = str.charAt(0);
if ((ch == '\r') || (ch == '\n')) {
return "";
}
return str;
}
int lastIdx = str.length() - 1;
char last = str.charAt(lastIdx);
if (last == '\n') {
if (str.charAt(lastIdx - 1) == '\r') {
lastIdx--;
}
} else if (last != '\r') {
lastIdx++;
}
return str.substring(0, lastIdx);
}
null) = null
StringUtils.chomp("") = ""
StringUtils.chomp("abc \r") = "abc "
StringUtils.chomp("abc\n") = "abc"
StringUtils.chomp("abc\r\n") = "abc"
StringUtils.chomp("abc\r\n\r\n") = "abc\r\n"
StringUtils.chomp("abc\n\r") = "abc\n"
StringUtils.chomp("abc\n\rabc") = "abc\n\rabc"
StringUtils.chomp("\r") = ""
StringUtils.chomp("\n") = ""
StringUtils.chomp("\r\n") = ""