Java中String类的几个常用方法

浅浅的花香味﹌ 2023-02-25 15:24 35阅读 0赞

关于String中的常用方法:

(1)public String toString()
这个方法就不用说了,字符串本身就是字符串,返回本身。

测试代码:

  1. System.out.println("abc".toString()); //输出:abc

(2)public char charAt(int index)
用于查找字符串中下标为index的字符,返回一个字符。

测试代码:

  1. System.out.println("student".charAt(0)); //输出:s
  2. System.out.println("student".charAt(6)); //输出:t
  3. //System.out.println("student".charAt(7)); //报错:StringIndexOutOfBoundsException异常,数组越界

(3)public int compareTo(String anotherString)
按照字典顺序比较两个字符串大小。

返回0表示两个字符串的值相同。

如果不相同,返回两个字符串 的 第一位不相同字符 的 ASCII之差。

如果是包含关系的话,返回长度之差。
例如:”abc”比较”abcsdads”等于-5,”bcdghk”比较”bcd”等于3。

测试代码:

  1. System.out.println("abc".compareTo("abc")); //输出:0
  2. System.out.println("abcd".compareTo("abcg")); //输出:-3
  3. System.out.println("abcf".compareTo("abcb")); //输出:4
  4. System.out.println("ab".compareTo("abcc")); //输出:-2
  5. System.out.println("ab".compareTo("abygdfg")); //输出:-5
  6. System.out.println("abygdfg".compareTo("ab")); //输出:5

(4)public boolean contains(CharSequence s)
判断前面的字符串是否包含后面的字字符串。

测试代码:

  1. System.out.println("hello World".contains("hello")); //输出:true
  2. System.out.println("hello World".contains("hello!")); //输出:false
  3. System.out.println("good".contains("goodjob")); //输出:false

(5)public boolean startsWith(String prefix)
判断当前字符串是否以prefix字符串开头。

public boolean endsWith(String suffix)
判断当前字符串是否以suffix字符串开结尾。

测试代码:

  1. System.out.println("hello world".startsWith("hel")); //输出:true
  2. System.out.println("hello world".endsWith("world")); //输出:true

(6)public boolean equals(Object anObject)
判断当前字符串内容是否与后面字符串内容相同。

注意:比较两个字符串内容是否相等不能使用“==”。

测试代码:

  1. System.out.println("abc".equals("abc")); //输出:true
  2. System.out.println("abc".equals("def")); //输出:false

(7)public boolean equalsIgnoreCase(String anotherString)
忽略大小写,判断当前字符串内容是否与后面字符串内容相同。

测试代码:

  1. System.out.println("abC".equalsIgnoreCase("ABc")); //输出:true

(8)public byte[] getBytes()
将字符串转成byte[]数组。

测试代码:

  1. byte[] bytes = "abcde".getBytes();
  2. for (byte x : bytes) {
  3. System.out.print(x + " "); //输出:97 98 99 100 101
  4. }

(9)public int indexOf(String str)
返回某个子字符串在当前字符串中第一次出现的下标,没有就返回-1。

public int lastIndexOf(String str)
返回某个子字符串在当前字符串中最后一次出现的下标,没有就返回-1。

测试代码:

  1. System.out.println("aaabbssddcdd".indexOf("dd")); //输出:7
  2. System.out.println("aaabbssddcdd".lastIndexOf("dd")); //输出:10

(10)public boolean isEmpty()
判断某个字符串是否为空字符串,已经实例化了,也就是字符串的长度是否为0。

测试代码:

  1. System.out.println("".isEmpty()); //输出:true
  2. System.out.println("abc".isEmpty()); //输出:false

(11)public int length()
返回当前字符串的长度。

注意:访问数组的长度是length属性,访问字符串的长度是length()方法。

测试代码:

  1. System.out.println("".length()); //输出:0
  2. System.out.println("abcde".length()); //输出:5

(12)public String replace(CharSequence target, CharSequence replacement)
将当前字符串当中的所有target字符串换成replacement字符串。

replaceFirst(CharSequence target, CharSequence replacement)
将当前字符串当中的第一个target字符串换成replacement字符串。

测试代码:

  1. System.out.println("year/month/day".replace("/", ".")); //输出:year.month.day
  2. System.out.println("http://www.baidu.com-http://".replaceFirst("http://", "https://")); //输出:https://www.baidu.com-http://

(13)public String[] split(String regex)
将当前字符串以regex字符串隔开,隔开后的片段以String[]形式返回。

测试代码:

  1. String[] ymd = "2020-1-1".split("-");
  2. for (String x: ymd) {
  3. System.out.print(x + " "); //输出:2020 1 1
  4. }

(14)public String substring(int beginIndex, int endIndex)
在当前字符串中,从beginIndex开始截取,截取到endIndex的新字符串,返回新字符串。

注意:beginIndex是包括的,endIndex是不包括的。
左闭右开:[beginIndex, endIndex) 或 [beginIndex, endIndex-1]。

测试代码:

  1. String str1 = "abcdefgh".substring(3, 6);
  2. System.out.println(str1); //输出:def

(15)public char[] toCharArray()
将字符串转换成char[]数组,并返回。

测试代码:

  1. char[] chars = "student".toCharArray();
  2. for (char c : chars) {
  3. System.out.print(c + " ");
  4. }

(16)public String toLowerCase()
将字符串全都转换成小写字母。

public String toUpperCase()
将字符串全都转换成大写字母。

测试代码:

  1. System.out.println("ABcDeFG".toLowerCase()); //输出:abcdefg
  2. System.out.println("abcDefG".toUpperCase()); //输出:ABCDEFG

(17)public String trim()
去除字符串前后的空格。

测试代码:

  1. System.out.println(" hello world ".trim()); //输出:hello world

(18)public static String valueOf(Object obj)
String类当中的一个静态方法,可以将任何类型转换成字符串,包括基本类型,引用类型。

先定义一个User类:

  1. class User {
  2. int id;
  3. String name;
  4. //构造方法
  5. public User() {
  6. }
  7. public User(int id, String name) {
  8. this.id = id;
  9. this.name = name;
  10. }
  11. //重写父类Object的toString()方法
  12. @Override
  13. public String toString() {
  14. return id + "-" + name;
  15. }
  16. }

测试代码:

  1. System.out.println(String.valueOf(100)); //输出:100
  2. System.out.println(String.valueOf(3.14)); //输出:3.14
  3. System.out.println(String.valueOf('a')); //输出:a
  4. System.out.println(String.valueOf(true)); //输出:true
  5. System.out.println(String.valueOf(new Object())); //输出:java.lang.Object@17a7cec2
  6. System.out.println(String.valueOf(new User())); //输出:0-null
  7. System.out.println(String.valueOf(new User(1001, "张三"))); //输出:1001-张三
  8. //String.valueOf()就是把一个类型,先调用这个类型的toString()方法,再以字符串返回

发表评论

表情:
评论列表 (有 0 条评论,35人围观)

还没有评论,来说两句吧...

相关阅读