12.Java的String类

//1.将字节数组里面的整数转换成字母
byte[] bys = {97,98,99};
String s = new String(bys);
System.out.println(s);   //输出 abc
//2.将单个字母拼接成单词
char[] cha = {'H','e','l','l','o'};
String s1 = new String(cha);
System.out.println(s1);    //输出 Hello
//3.实际开发中可以String类可以免new
String s2 = "abc";
//4.比较两个字符串是否相等,区分大小写
String s2 = "abc";
String s3 = "ABC";
boolean eq = s2.equals(s3);
System.out.println(eq);  //输出 false
//5.不区分大小写比较两个字符串是否相等
boolean eq1 = s2.equalsIgnoreCase(s3);
System.out.println(eq1);  // 输出 true
//6.判断字符串是不是从字符a开头的
boolean b = s2.startsWith("a");
System.out.println(b);  //输出 true
//7.判断字符串是不是以字符c结尾的
boolean b1 = s2.endsWith("c");
System.out.println(b1);  //输出 true
//8.判断字符串对象s2是不是空字符串
boolean em = s2.isEmpty();
System.out.println(em); //输出 false
//9.判断字符串对象s2的长度
int length = s2.length();
System.out.println(length);  //输出3
//10.获取字符串对象s2索引1位置的字符
char c = s2.charAt(1);
System.out.println(c);  //输出b
//11.获取字符串对象s2字符c所在位子的索引
int c1 = s2.indexOf("c");
System.out.println(c1);  //输出 2
//12.获取字符串对象s4字符b最后出现所在位子的索引
String s4 = "abcabc";
int m1 = s4.lastIndexOf("b");
System.out.println(m1);  //输出 4
//13.截取字符串对象s4字符从索引2开始以后的所有字符
String sub1 = s4.substring(2);
System.out.println(sub1);  //输出cabc
//14.截取字符串对象s4字符从索引2开始4结束,不包含4的所有字符
String sub2 = s4.substring(2, 4);
System.out.println(sub2); //输出 ca
//15.将字符串对象s2(“abc”)转换成字节数组
byte[] by = s2.getBytes();
for (int i = 0; i < by.length; i++) {
	System.out.println(by[i]);  //输出 97  98   99  中间是换行符
}
//16.将字符串对象s2(“abc”)转换成字符数组
char[] chars = s2.toCharArray();
for (int i = 0; i < chars.length; i++) {
	System.out.println(chars[i]);  //输出 a,b,c  中间是换行符
}
//17.将指定类型的数据转换成字符串类型数据
int a = 123;
String s5 = String.valueOf(a);
System.out.println(s5);  //输出字符串类型的 “123”
//18.将字符串对象s2("abc")的字符b替换为d
String re = s2.replace("b", "d");
System.out.println(re);  //输出 adc
//19.将字符串对象s2("abc")以空格进行切割,返回切割后的字符串数据,原字符串不受影响
String[] m2 = s2.split(" ");
for (int i = 0; i < m2.length; i++) {
	System.out.println(m2[i]);  //输出 abc
}
//20.去除字符串sb对象两边的空格
String sb = "  a b c ";
String tri = sb.trim();
System.out.println(tri);  //输出 a b c
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡启行

您的鼓励将是我前进的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值