【源码解析】String、StringBuilder、StirngBuffer

String

基础知识

  • String是不可变字符串,final修饰的类,不可被继承。
  • 内部定义了final char[] value存储字符串数据。
  • 实现了Serializable,Comparable,CharSequence接口
  • String重写了hashCode方法,采用多项式计算得到。不相同的字符串也会得到同样的hash。
  • Object类中的hashCode方法是一个Native调用。
  • 重写了equals,改为内容的判断
  • String str = "abc"存放在String常量池中
  • String str = new String(abc)存放在堆中。
  • 当str改变值时,会新建一个改变后的字符串,将地址赋给子字符串的引用。

常用方法

  • int length()//返回String字符串的长度
  • boolean isEmpty()//字符串长度是否为0
  • char charAt(int index)//返回index位置的字符,index超过则抛出异常
  • codePointAt(int index)//返回字符(Unicode代码点)在指定的索引
  • int compareTo(String anotherString)//比较两个字符串的字典顺序大小。
  • String concat(String sr)//将指定的字符串,连接到本字符串末尾。
  • boolean contains(CharSequence s)//字符串包含给定字符串,则返回true
  • equals(Object anObject)与指定的对象比较
  • byte[] getBytes(Charset charset)将String使用给定的编码编码为字节数组。
  • int hashCode()//返回此字符串的哈希代码
  • int indexOf(int ch)//返回指定字符在字符串中第一次出现的位置。
  • int lastIndexOf(char ch)//从后面开始查找ch,返回在str中的正序坐标
  • String replace(char oldChar, char newChar)//返回从字符串中替换oldChar为newChar的新字符串
  • String replaceAll(String regex, String replacement)//将str中regex字符串替换为replacement
  • String replaceFirst(String regex, String replacement)//替换第一个regex为replacement
  • String[] split(String regex)//将regex分割字符串为数组,不存储regex
  • CharSequence subSequence(int beginIndex, int endIndex)//返回字符串子序列
  • String substring(int beginIndex)//返回一个字符串的子串。默认beginindex开始
  • char[] toChaArray() //将字符串转换为一个新的字符数组
  • String trim() //返回一个字符串,删除前后空格
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值