Java语言-9:String类的特点及equals方法和String类的转换功能

1、String:表示字符串:
字符串是常亮;它们的值在创造后不能更改
String是一种 特殊的引用类型
默认值:null(int 型的默认值为0;Boolean型的默认值为false)
基本类型和引用类型的区别:
基本类型储存在栈内存中,是静态的字段,会自动释放;
引用类型储存在堆内存中,是动态字段,相当于一个地址,不会自动释放。
构造方法:
String():无参构造
String(byte[]bytes):将字节数组转换成字符串(通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。)
例如:
byte[]bytes = {100,50,27,35};
String s = new String(bytes);
即将字符数组bytes装换成字符串String s.
public String(char[]value):将字节数组转换成字符串(分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。)
例如:
char[]ch = {'a','b','c','d'.'e'};
String s1 = new String(ch);
System.out.print(ch);
输出结果为:abcde 
public String(byte[]bytes,int index,int length):将字节数组的一部分转换成字符串
例如:   char[]ch1 = {'a','b','c','d'}
String s2 = new String(bytes,2,3)
System.out.print(s2);
输出结果为:cd
toCharArray():将字符串转换成字符
public String(char[]value,int index,int count):将字符数组的一部分转换成字符串
public String(String original):将一个字符串串常量构成一个字符串对象
常用的方法:
public int length()返回次字符串的长度
注意:数组中没有length()方法,只有length属性
字符串中有length()
集合中没有length(),获取集合的元素数:size()
2、String类的常用判断功能
    1)Boolean equals(object obj):将此字符串与指定的对象比较
例如:String s = "haha";
String s1 = "hehe";
System.out.print(s.equals(s1));
输出结果为:false
   2)Boolean equalsIgnoreCase(将此String与另一个String比较,不考虑大小写)
String s = "hello";
String S = "HELLO";
System.out.print(s.equalsIgnoreCase(S))
输出结果为:true
   3)Boolean contains(String str):判断此大字符串中是否包含子字符串(重点)
String s = "hello";
String S = "ell";
String s1 = "jdk"
System.out.print(s.contains(S));
输出结果为:true
System.out.print(s.contians(s1));
输出结果为:false
   4)Boolean startWith(String str):是否以当前str字符串开头(重点)
String s = "he";
String s1 = "hello";
String s2 = "word";
System.out.print(s.startWith(s1));
输出为:true
System.out.print(s.startWith(s2));
输出为:false
   5)Boolean endsWith(String str):是否已当前str字符串结尾(重点)
String s = "lo";
String s1 = "hello";
String s2 = "shift";
System.out.print(s.endsWith(s1));
输出为:true
System.out.print(s.endsWith(s2));
输出为:false
   6)Boolean isEmpty():判断字符串为空
String s = "";
String s1 = "";
String s2 = "a";
System.out.print(s.isEmpty(s1));
输出为:true
System.out.print(s.isEmpty(s2));
输出为:false
   7)public String concat(String str):拼接功能,等同于连字符
3、==和equals的区别:
==:比较的是地址值
  equals:默认的比较是地址值,String底层重写了equals方法,所有比较的内容是否相同
4、String类的常用获取功能:(方法)
 
    1)public int length():获取字符串的长度
例如:
String s = "hello";
public int length();
System.out.print(s.length());
输出结果为:5
   2) public char charAt(int index)返回指定索引处的 字符
System.out.print(s.charAt(3))
输出结果为:l
   3) public int indexOf(int ch)返回指定字符在此字符串中第一次出现处的索引
System.out.print(s.indexOf('e'));
输出结果为:1(数字)
  问题:既然传入的字符:这里为什么用int类型
  在ask码中,97和‘a’表示同一个
   4)public int indexOf(int ch,int fromIndex)返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。(即一共出现了几次)
System.out.print(s.indexOf('l',2));
输出结果为:2
   5) public int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引
System.out.print(s.indexOf("ell"));
输出结果为:1
System.out.print(s.indexOf("ll"));
输出结果为:2
   6)public int indexOf(String str,int fromIndex)回在此字符串中第一次出现指定字符串处的索引,从指定的索引开始搜索。
System.out.println(s.indexOf("llo"));
输出结果为:2
 
 5、 截取功能
  
  1)public String substring(int beginIndex):从指定位置开始截取,默认截取到末尾,返回新的字符串
String s = "happy"
System.out.print(s.substring('2'));
输出为:ppy
  2)public String substring(int beginIndex, int endIndex):从指定位置开始到指定位置末尾结束,包前不包后

System.out.print(s.substring(1,3))

        输出结果为:app

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值