String类 字符串的生成和关于字符串的常用方法总结

String类 字符串的生成和关于字符串的常用方法总结

一、字符串的特点:

  1. 字符串的内容永不可变
  2. 正因为字符串不可改变,所以字符串师可以共享使用的。
  3. 字符串效果上相当于char[ ] 字符数组,但是底层原理师byte[ ]字节数组。
  4. 对于字符串是引用类型,使用 == 是进行地址值的比较,内容比较应该使用equals()

二、字符串的生成方法
三种构造方法和一种直接创建

  1. pubic String(),创建一个空白字符串
  2. public String(char [ ] arr),根据字符数组的内容来创建对应的字符串
  3. public String(byte[ ] arr ),根据字节数组的内容,来创建对应的字符串
  4. String str = “ ”;直接创建;
public static void main(String[] args) {
		// pubic String(),创建一个空白字符串
		String str1 = new String();
		
//		public String(char [ ] arr),根据字符数组的内容来创建对应的字符串
		char[] c = {'c','b','a'};
		String str2 = new String(c);
		
//		public  String(byte[ ] arr ),根据字节数组的内容,来创建对应的字符串
		
		byte[] b = {97,98,99};
		String str3 = new String(b);
		
//		String str4 = "bca";   直接创建
		String str4 = "bca";
		
		System.out.println(str1);
		System.out.println(str2);
		System.out.println(str3);
		System.out.println(str4);
		
	}

三、常用的相关方法

返回类型方法
intlength()
Stringconcat(String str)
charcharAt(int index)
intindexof(String str)
  1. length() 获取字符串中含有字符个数,拿到字符串的长度。
  2. concat(String str) 将两个字符串拼接成新的字符串。
  3. chatAt(int index) 获取指定索引位置的单个字符。
  4. indexOf(String str)
public static void main(String[] args) {
	
	//获取字符串的长度
	String str = "flakjflkaj";
	System.out.println("字符串的长度:"+str.length());//10
	
	//拼接字符串
	String str1 = "Hello";
	String str2 = "World";
	String str3 = str1.concat(str2);//HelloWorld
	System.out.println(str3);
	System.out.println("+++++++++");
	//获取指定索引位置的单个字符
	char c = str1.charAt(1);
	System.out.println("在1号索引位置的字符是:"+c);//e
	System.out.println("==========");
	
	//查找参数字符串在本来字符串当中出现的第一次索引位置
	//如果根本没有,返回-1值
	int index = str3.indexOf("llo");
	System.out.println(index);//2
	
	
	}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值