我是靠谱客的博主 留胡子铃铛,这篇文章主要介绍String类的常用方法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)方法主要用来将指定字符串连接到此字符串的结尾,注意其并没有改变原来的字符串长度。

最后

以上就是留胡子铃铛最近收集整理的关于String类的常用方法String类的常用方法的全部内容,更多相关String类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部