String方法总结

1.charAt()    返回指定索引处的字符。索引范围为从 0 到 length() - 1

		String s="1234567890";
		char t=s.charAt(9);
		System.out.println(t);
        //输出t=0

2.equals  比较两个字符串是否完全相同

	String s1="aaa";
	String s2="bbb";
	String s3="aaa";
	boolean t1;
	boolean t2;
	t1=s1.equals(s2);
	t2=s1.equals(s3);
	System.out.println(t1);
	System.out.println(t2);
    //t1返回false
    //t2返回true

3.equalsIgnoreCase  不考虑大小写比较两个字符串是否完全相同

String s1="aaaBBBccD";
		String s2="AAAbbbCCD";
		boolean t1;
		boolean t2;
		t1=s1.equals(s2);
		t2=s1.equalsIgnoreCase(s2);
		System.out.println(t1+"--"+t2);
        //t1为false
        //t2为true

4.getBytes() 方法有两种形式

getBytes(String charsetName): 使用指定的字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

getBytes(): 使用平台的默认字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

		String s1=new String("abcd");
		String s2=s1;
		byte[] a1=s1.getBytes("UTF-8");
		byte[] a2=s2.getBytes();
		System.out.println(a1);
		System.out.println(a2);
        //a1输出结果为[B@53606bf5
        //a2输出结果为[B@5f4fcc96

5. length()字符串长度

    String s="abcde";
	System.out.println(s.length());
    //长度为5

6. getchars()截取多个字符并由其他字符串接收

    String s="hello world!";
	char[] b=new char[10];
	s.getChars(6, 12, b, 0);
	System.out.println(b);
    //输出 world!

其中第一个参数6是要截取的字符串的初始下标(int sourceStart),第二个参数12是要截取的字符串的结束后的下一个下标(int sourceEnd)也就是实际截取到的下标是int sourceEnd-1,第三个参数是接收的字符串(char target[]),最后一个参数是接收的字符串开始接收的位置。

7.toCharArray()将字符串变成一个字符数组

	String s="hello world";
	char[] t=s.toCharArray();
	System.out.println(t);
    //输出为s的数组

8.startsWith()和endsWith()判断字符串是不是以特定的字符开头或结束
 

    String s="hello world";
	boolean t1=s.endsWith("ld");
	boolean t2=s.startsWith("ee");
	System.out.println(t1+"---"+t2);
    //t1为true,t2位false

9.toUpperCase()和toLowerCase()将字符串转换为大写或小写

    String s1="Hello World";
	String s2=s1.toLowerCase();
	String s3=s1.toUpperCase();
	System.out.println(s2);
	System.out.println(s3);
    //s2为hello world,s3为HELLO WORLD

10.concat() 连接两个字符串

	String s1="hell";
	String s2="oworld";
	String s3=s1.concat(s2);
	System.out.println(s3);
    //输出结果为helloworld

11.trim()去掉起始和结束的空格

    String s1="    hello world     ";
	String s2=s1.trim();
	System.out.println(s2);
    //输出结果为hello world

12.substring()截取字符串

    String s1="hello world";
	String s2=s1.substring(4);//截取[4,最后) 包括4到最后
	String s3=s1.substring(2,7);//截取[2,7)  包括2但不包括7
	System.out.println(s2+"---"+s3);
    //s2输出为o world
    //s3输出为llo wo

13.indexOf()和lastIndexOf()前者是查找字符或字符串第一次出现的地方,后者是查找字符或字符串最后一次出现的地方

	String s1="hello world!";
	int a=s1.indexOf("l");
	int b=s1.lastIndexOf("l");
	System.out.println(a+"---"+b);
    //a输出为2,b输出为9

14.replace() 替换

    String a="hello world";
	String b="你好";
	System.out.println(a.replace(a, b));
	System.out.println(a.replace("hello", "嗨"));
	System.out.println(b.replace("你", "大家"));
    //输出结果为:你好
    //         :嗨 world
    //         :大家好

15.compareTo()和compareToIgnoreCase()按字典顺序比较两个字符串的大小,前者区分大小写,后者不区分

    String a="hello world";
	String b="HELLO WORLD";
	System.out.println(a.compareTo(b));
	System.out.println(a.compareToIgnoreCase(b));
    //第一个输出为32
    //第二个输出为0,两个字符串在字典顺序中大小相同,返回0。

//当前者大于后者时输出>0的数,小于时输出<0的数(字典的顺序:0123456ABCabc 大写字母小写字母前面)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值