• 字符串类


    字符串相关类

    String类,构造字符串对象
    • 常量对象,使用双引号括起来的字符序列,字符串一旦创建,不能改变
    • 字符串的字符使用Unicode字符编码,一个字符占两个字节
    • 可以简单理解为是一个字符数组
    • String是一个Final类,代表不可变的字符序列
    • new和+等产生新对象的在gc区,
    • 常量相加,在常量区
    • 字符常量在方法区的常量区
    • intern();把string压入常量池,若有则不会添加。
      • 字面量在常量区
      • 对各常量相加结果总是常量区
      • 多个字符串相加,里面若有一个变量则在gc区
      • new创建对象,在gc区
    • 原理:
      • 字符串
      • 常用方法
        • 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.

    //1.8.0_181版 jdk中String类源码第114行
    private final char value[];     
    
    //String 部分源码
    /**
         * Converts this string to a new character array.
         *
         * @return  a newly allocated character array whose length is the length
         *          of this string and whose contents are initialized to contain
         *          the character sequence represented by this string.
         */
        public char[] toCharArray() {
            // Cannot use Arrays.copyOf because of class initialization order issues
            char result[] = new char[value.length];
            System.arraycopy(value, 0, result, 0, value.length);
            return result;
        }
    /**
         * Compares this string to the specified object.  The result is {@code
         * true} if and only if the argument is not {@code null} and is a {@code
         * String} object that represents the same sequence of characters as this
         * object.
         *
         * @param  anObject
         *         The object to compare this {@code String} against
         *
         * @return  {@code true} if the given object represents a {@code String}
         *          equivalent to this string, {@code false} otherwise
         *
         * @see  #compareTo(String)
         * @see  #equalsIgnoreCase(String)
         */
        public boolean equals(Object anObject) {
            if (this == anObject) {
                return true;
            }
            if (anObject instanceof String) {
                String anotherString = (String)anObject;
                int n = value.length;
                if (n == anotherString.value.length) {
                    char v1[] = value;
                    char v2[] = anotherString.value;
                    int i = 0;
                    while (n-- != 0) {
                        if (v1[i] != v2[i])
                            return false;
                        i++;
                    }
                    return true;
                }
            }
            return false;
        }
    

    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) 修改

    StringBuilder类

    • StringBuffer比较老的版本 特点,线程安全,速度慢
    • StringBuilder比较新的版本 特点,速度快,性能好
    • StringBuffer的增强,比stringbuffer效率高,使用一样。
  • 相关阅读:
    数据库外连接和内连接详解
    关于省市联动问题的分析
    邮箱验证修改密码,通过邮箱找回密码
    格式化Json传递的日期
    项目中验证码的使用
    水仙花数
    回文数
    冒泡排序
    《终结者·洛谷小说》总集
    题解 CF151A 【Soft Drinking】
  • 原文地址:https://www.cnblogs.com/refengqingfu/p/9978846.html
Copyright © 2020-2023  润新知