Java中String类的几个常用方法

关于String中的常用方法:

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

测试代码:

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

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

测试代码:

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

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

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

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

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

测试代码:

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

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

测试代码:

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

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

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

测试代码:

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

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

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

测试代码:

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

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

测试代码:

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

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

测试代码:

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

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

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

测试代码:

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

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

测试代码:

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

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

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

测试代码:

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

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

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

测试代码:

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

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

测试代码:

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

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

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

测试代码:

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

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

测试代码:

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

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

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

测试代码:

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

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

测试代码:

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

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

先定义一个User类:

class User {
    int id;
    String name;

    //构造方法
    public User() {

    }
    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    //重写父类Object的toString()方法
    @Override
    public String toString() {
        return id + "-" + name;
    }
}

测试代码:

System.out.println(String.valueOf(100));  //输出:100
System.out.println(String.valueOf(3.14));  //输出:3.14
System.out.println(String.valueOf('a'));  //输出:a
System.out.println(String.valueOf(true));  //输出:true
System.out.println(String.valueOf(new Object()));  //输出:java.lang.Object@17a7cec2
System.out.println(String.valueOf(new User()));  //输出:0-null
System.out.println(String.valueOf(new User(1001, "张三")));  //输出:1001-张三
//String.valueOf()就是把一个类型,先调用这个类型的toString()方法,再以字符串返回
  • 47
    点赞
  • 409
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值