每日一題 - String類的常用方法

String這個對象在Java中應用的地方非常多,它自身也提供了許多方法。下文,總結了String的常用方法。

charAt():返回char指定索引處的值。

String str = "qfcwx.top";
System.out.println(String.format("charAt():%s", str.charAt(2)));

輸出結果:

charAt():c

codePointAt():返回指定索引處的字符(Unicode代碼點)

System.out.println(String.format("codePointAt():%s", str.codePointAt(2)));

輸出結果:

codePointAt():99

indexOf():返回指定字符第一次出現的字符串內的索引,如果不存在,則返回-1

System.out.println(String.format("indexOf():%s", str.indexOf('t')));

輸出結果:

indexOf():6

length():返回字符串的長度

System.out.println(String.format("length():%d", str.length()));

輸出結果:

length():9

hashCode():返回此字符串的哈希碼

System.out.println(String.format("hashCode():%d", str.hashCode()));

輸出結果:

hashCode():481831734

substring():截取字符串,返回一個字符串,該字符串是此字符串的子字符串

System.out.println(String.format("substring():%s", str.substring(2)));
System.out.println(String.format("substring():%s", str.substring(0, 5)));

輸出結果:

substring():cwx.top
substring():qfcwx

replace():字符串替換

String aStr = "/upload//file//image.png";
System.out.println(String.format("replace():%s", aStr.replace("//", "/")));

輸出結果:

replace():/upload/file/image.png

trim():去除字符串兩端空白

String tStr = "  qfcwx.top  ";
System.out.println(String.format("trim():之前:%s,長度:%d", tStr, tStr.length()));
System.out.println(String.format("trim():之後:%s,長度:%d", tStr.trim(), tStr.trim().length()));

輸出結果:

trim():之前:  qfcwx.top  ,長度:13
trim():之後:qfcwx.top,長度:9

split():分割字符串,返回一個字符串數組

String sStr = "1,2,3,4,5,6";
String[] splitArr = sStr.split(",");
System.out.println(String.format("split():%s", Arrays.toString(splitArr)));

輸出結果:

split():[1, 2, 3, 4, 5, 6]

getBytes():返回字符串的byte類型數組

String byteStr = "AaBbCc";
byte[] bytes = byteStr.getBytes();
// 輸出爲每個字節的ASCII值
System.out.println(String.format("getBytes():%s", Arrays.toString(bytes)));

輸出結果:

getBytes():[65, 97, 66, 98, 67, 99]

toLowerCase() | toUpperCase():將字符串轉成小寫 | 將字符串轉成大寫

String toStr = "This is my exclusive Moment";

System.out.println(String.format("toLowerCase():%s", toStr.toLowerCase()));

System.out.println(String.format("toUpperCase():%s", toStr.toUpperCase()));

輸出結果:

toLowerCase():this is my exclusive moment
toUpperCase():THIS IS MY EXCLUSIVE MOMENT

equals():字符串比較(區分大小寫)

String str1 = "Admin";
String str2 = "admin";
String str3 = "admin";
System.out.println(String.format("equals():%s", str1.equals(str2)));
System.out.println(String.format("equals():%s", str2.equals(str3)));

輸出結果:

equals():false
equals():true

equalsIgnoreCase():字符串比較(忽略大小寫)

System.out.println(String.format("equalsIgnoreCase():%s", str2.equalsIgnoreCase(str1)));

輸出結果:

equalsIgnoreCase():true

startsWith():判斷此字符串是否以指定的前綴開頭

String imagePath = "upload/a.jpg";
System.out.println(String.format("startsWith():%s", imagePath.startsWith("upload")));

輸出結果:

startsWith():true

endsWith():判斷此字符串是否以指定的後綴結尾

String fileName = "pig.png";
System.out.println(String.format("endsWith():%s", fileName.endsWith(".jpg")));

輸出結果:

endsWith():false

valueOf(boolean b | char c | char[] data | double d | float f | int i | long l | Object obj): 返回 param 參數的字符串 param 形式。 param = 當前參數的類型

boolean falg = true;
long num = 123456L;
float price = 12.56F;
double money = 12D;

System.out.println(String.format("valueOf()-boolean:%s", String.valueOf(falg)));
System.out.println(String.format("valueOf()-long:%s", String.valueOf(num)));
System.out.println(String.format("valueOf()-float:%s", String.valueOf(price)));
System.out.println(String.format("valueOf()-double:%s", String.valueOf(money)));

輸出結果:

valueOf()-boolean:true
valueOf()-long:123456
valueOf()-float:12.56
valueOf()-double:12.0

開發中常用的方法就總結到這裏,其實還有一些方法,博主這裏沒有說到,感興趣的讀者,可以去看Java的API文檔:

http://www.matools.com/api/java8

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章