每日一题 - 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

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