String类的常用方法

String类的常用方法

String类是Java中一个非常实用的类,它是一个封装char[]数组的对象,一个String类型的字符串一旦被创建,长度就不可以改变。

1、String常见方法----length();

public class Main{
	public static void main(String[] args){
		String s = "abcde";
		System.out.println(s.length());
	}
}

在这里插入图片描述
length()方法主要是用来返回字符串的长度,返回值是int类型。

2、String常见方法----charAt(int index);

public class Main{
	public static void main(String[] args){
		String s = "abcde";
		System.out.println(s.charAt(1));
	}
}

在这里插入图片描述
charAt(int index)方法主要用来返回指定索引初的char值,返回值是char类型。

3、String常见方法----indexOf(int ch);

public class Main{
	public static void main(String[] args){
		String s = "abcded";
		System.out.println(s.indexOf("d"));
	}
}

在这里插入图片描述
indexOf(int ch)方法主要用来返回指定字符在此字符串中第一次出现处的索引,返回值是int类型。

4、String常见方法----lastIndexOf(int ch)

public class Main{
	public static void main(String[] args){
		String s = "abcded";
		System.out.println(s.lastIndexOf("d"));
	}
}

在这里插入图片描述
lastIndexOf(int ch)方法主要用来返回指定字符在此字符串中最后一次出现处的索引,返回值是int类型。

5、String常见方法—concat(String str)

public class Main{
	public static void main(String[] args){
		String s = "abcded";
		System.out.println(s.concat("xyz"));
		System.out.println(s);
	}
}

在这里插入图片描述

concat(String str)方法主要用来将指定字符串连接到此字符串的结尾,注意其并没有改变原来的字符串长度。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值