String类中常用的方法

1.charAt(int index)返回int

返回索引处的char值

public static void main(String[] args) {
	char c = "中国人".charAt(1);
	System.out.println(c);
}

输出结果:国

2.compareTo(String anotherString)返回int

按照字典(ASCII)的顺序比较两个字符串

public static void main(String[] args) {
	System.out.println("abc".compareTo("abc")); //0
	System.out.println("abc".compareTo("abcd"));   //-1
	System.out.println("abc".compareTo("ab"));  //1
	System.out.println("A".compareTo("a")); //-32
	System.out.println("aDc".compareTo("adC"));// -32
	System.out.println("b".compareTo("K"));// 23
}

输出结果:
0
-1
1
-32
-32

ASCII值:D-->68 b-->100

3.contains(CharSequence s)返回boolean

判断一个字符串中是否包含该字符串,也就是判断"abcd"中是否包含"a"

public static void main(String[] args) {
	System.out.println("abcd".contains("a"));// true
	System.out.println("abcd".contains("k"));// false
}

输出结果:true false

4.endsWith(String suffix)返回boolean

判断当前字符串是否以某个字符串结尾

public static void main(String[] args) {
	System.out.println("HelloWorld".endsWith("ld"));//true
	System.out.println("hello.java".endsWith(".txt"));//false
}

输出结果:true false

5.equalsIgnoreCase(String anotherString)返回boolean

判断两个字符串是否相等,忽略大小写

	System.out.println("ABc".equalsIgnoreCase("abc"));//true

输出结果:true

6.getBytes()返回byte[]

将字符串转换成字节数组

public static void main(String[] args) {
	byte[] s = "hello".getBytes();
	for (int i = 0; i < s.length; i++) {
		System.out.println(s[i]);
	}
}

输出结果:
104
101
108
108
111

7.indexOf(String str)返回int

判断某个字符串在当前字符串中第一次出现时的下标

	System.out.println("abcdefghijk".indexOf("e"));//4
    System.out.println("abcdefghijk".indexOf("hij"));//7
    System.out.println("abcdefghijk".indexOf("GHUIK"));//-1

输出结果:
4
7
-1

8.isEmpty()返回boolean

判断某个字符串是否为空

public static void main(String[] args) {
	String s = "";
	String s1 = "aaa";
	String s2 = null;
	System.out.println(s.isEmpty());//true
	System.out.println(s1.isEmpty());//false
	System.out.println(s2.isEmpty());//空指针异常
}

输出结果:
true
false
空指针异常

9.length()返回int

返回某个字符串的长度

public static void main(String[] args) {
	String s1 = "aaa";
	System.out.println(s1.length());//3
}

输出结果:3

10.lastIndexOf(String str)返回int

判断某个字符串在当前字符串中最后一次出现时的下标

public static void main(String[] args) {
	System.out.println("我是中国人中国人中国人".lastIndexOf("国"));//9
	System.out.println("我是中国人中国人中国人".lastIndexOf("人中"));//7
	System.out.println("我是中国人中国人中国人".lastIndexOf("北京"));//-1
}

输出结果:9 7 -1

11.replace(CharSequence target, CharSequence replacement) 返回String

String的 父接口就是CharSequence
字符串替换

public static void main(String[] args) {
	String s = "http://www.baidu.com";
	System.out.println(s);
	System.out.println(s.replace("http:","https:"));
}

输出结果:
http://www.baidu.com
https://www.baidu.com

12.split(String regex) 返回String[]

拆分字符串

public static void main(String[] args) {
	String s = "1980-10-1";
	String[] a = s.split("-");
	for (int i = 0; i < a.length; i++) {
		System.out.println(a[i]);
	}
}

输出结果:
1980
10
1

13.startsWith(String prefix)返回boolean

判断某个字符串是否以某个字符串开始

public static void main(String[] args) {
	String  s = "helloworld";
	System.out.println(s.startsWith("he"));
	System.out.println(s.startsWith("el"));
}

输出结果:true false

14.substring(int beginIndex,int endIndex)返回String

有两种传参方式
1.从指定下标截取字符串2.可设置起始结束位置截取字符串

public static void main(String[] args) {
	String  s = "http://www.baidu.com";
	System.out.println(s.substring(7));
	System.out.println(s.substring(7,10));
}

输出结果:
www.baidu.com
www

15.toCharArray()返回char[]

将字符串转换为char数组

public static void main(String[] args) {
	String  s = "abcdefg";
	char[] a = s.toCharArray();
	for (int i = 0; i < a.length; i++) {
		System.out.println(a[i]);
	}
}

输出结果:
a
b
c
d
e
f
g

16.toLowerCase()返回String

转换为小写

public static void main(String[] args) {
	String  s = "ABCDEFG";
	System.out.println(s.toLowerCase());
}

输出结果:abcdefg

17.toUpperCase()返回String

转换为大写

public static void main(String[] args) {
	String  s = "abcdefg";
	System.out.println(s.toUpperCase());
}

输出结果:ABCDEFG

18.trim()返回String

注意:只能去除前后空白
去除字符串前后空白

public static void main(String[] args) {
	String  s = "                 hello     world                    ";
	System.out.println(s.trim());
}

输出结果:hello world

19.valueOf()返回String

String类中唯一的静态方法
万物皆可字符串(将非字符串转换为字符串)

public class test {
    public static void main(String[] args) {
        String a = String.valueOf(123);
        String b = String.valueOf(true);
        String c = String.valueOf(3.1415);
        String d = String.valueOf(new customer());//实际上是调用了toString()方法
        String e = String.valueOf(new user());//重写过后的toString()方法
        System.out.println(a+"\n"+b+"\n"+c+"\n"+d+"\n"+e);
    }
}
class customer{

}
class user{
    @Override
    public String toString() {
        return "重写过后的toString()方法";
    }
}

输出结果:
123
true
3.1415
customer@1b6d3586
重写过后的toString()方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值