String相关类

String

  • String类,构造字符串对象
    • 常量对象,使用双引号括起来的字符序列,字符串一旦创建,不能改变
    • 字符串的字符使用Unicode字符编码,一个字符占两个字节
    • 内置final修饰的字符数组,String主要的行为就是管理内置字符数组
    • String是一个Final类,代表不可继承更改的对象
    • 新建String内存分配
      • 字符常量在方法区的常量区
        • 常量相加,在常量区
      • new关键字创建的皆在gc区
        • new创建的String类型和任意类型做拼接皆在gc区
      • intern();把string压入常量池,若有则不会添加。
    • 类声明:
    //源码111行112行
    public final class String
      implements java.io.Serializable, Comparable<String>, CharSequence {
    
    • 主要属性:
    //源码114行
    private final char value[];
    
    • 常用方法
      • int length()
        • Returns the length of this string.
      • char charsAt(int index)
        • Returns the char value at the specified(指定的) index.
      • char[] toCharArray()
        • Converts(转换)this string to a new character array.
      • boolean equals(Object anObject)
        • Compares this string to the specified(指定的) object.
      • int compareTo(String anotherString)
        • Compares two strings lexicographically(字典序).
      • int indexOf(String str) 返回str在字符串中出现的下标
        • Returns the index within this string of the first occurrence of the specified(指定) substring(子串).
      • int indexOf(String str, int fromIndex)
        • Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
      • int lastIndexOf(String str)
        • Returns the index within this string of the last occurrence of the specified substring.
      • int lastIndexOf(String str, int fromIndex)
        • Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
      • boolean startsWith(String prefix, int toffset)
        • Tests if the substring of this string beginning at the specified index starts with the specified prefix.
      • boolean endsWith(String suffix)
        • Tests if this string ends with the specified suffix.
      • String substring(int beginIndex, int endIndex) 截取子串
        • Returns a string that is a substring of this string.
      • String substring(int beginIndex) 截取子串
        • Returns a string that is a substring of this string.
      • String replace(char oldChar, char newChar) 替换
        • Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
      • String replaceAll(String regex, String replacement) 替换字符串
        • Replaces each substring of this string that matches the given regular expression with the given replacement.
      • String trim() 修剪首尾空白字符(小于等于32的)
        • Returns a string whose value is this string, with any leading(首) and trailing(尾) whitespace(空白字符) removed.
      • String toUpperCase() 转大写
        • Converts all of the characters in this String to upper case using the rules of the default locale.
      • String toLowerCase() 转小写
        • Converts all of the characters in this String to lower case using the rules of the default locale.
      • String[] split(String regex) 切割
        • Splits(切割) this string around matches of the given regular(正则表达式) expression.
      • boolean equalsIgnoreCase(String anotherString) 比较两个字符串是否一致,忽略大小写
        • Compares this String to another String, ignoring(忽略) case considerations.

StringBuffer

  • 内容可变字符串序列
  • 很多方法和String相同,区别是长度可变
  • 容器,装载类型为char
  • 构造器
    • StringBuffer() 无参
      • Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
    • StringBuffer(String str)
  • 常用方法
    • append() 多态,方法很多,可认为时可添加任意类型,直接修改StringBuffer本身
      • 返回值为本身,可以连用append来,无参构造预置数组为16;
    • insert(int, Objects) 插入,任意数据类型
    • reverse() 反转
    • delete(int,int) 删除
    • setCharAt(int,Objects) 修改
  • StringBuffer比较老的版本 特点,线程安全,速度慢
  • StringBuilder比较新的版本 特点,速度快,性能好

StringBuilder类

  • StringBuffer的增强,比stringbuffer效率高,完全兼容StringBuffer(使用一致),线程不安全。

猜你喜欢

转载自blog.csdn.net/qq_34548401/article/details/84919860