java - String

String

interface

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
  • Serializable - 序列化(标识接口)
  • Comparable<String> - 比较,可用Collections类的静态方法 sort 进行自动排序
  • CharSequence - 字符序列的统一接口,提供字符序列通用的操作方法
interface CharSequence{
    charAt(int i);
    length();
    subSequence(int start,int end);
    toString();
}

attribute

private final char value[];
private int hash;
public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();
 //静态内部类 - 单例,内置一个compare方法,用于进行忽略大小写比较

method

//String是不可变类,所有对String的操作,实际都产生了一个新的String对象

//4.3.1 字符串比较方法
equals(Object):boolean             //与字符串等值比较 
equalsIgnoreCase(Object):boolean   //不分大小写 返回true or false
compareTo(String):int              //与字符串比较 
compareToIgnoreCase(String):int    //不分大小写

contains(CharSequence):boolean     //包含参数字符串
startsWith(String):boolean         //有参数字符串作为前缀
endsWith(String):boolean           //有参数字符串作为后缀
indexOf(s1):int                    //查找参数字符串的起始索引

//4.3.2 字符串操作方法
length():int               //获取字符串长度
toCharArray():char[]       //生成一个字符数组,
charAt(int):char           //获取指定位置的字符
substring(beginIndex,endIndex):String    //返回开始下标到结束下标-1的子串

replace(regex,newString):String          //模式匹配替换
replaceFirst(regex,newString):String     //只替换一次
split(regex):String[]      //按模式分割字符串
concat(String):String      //与字符串连接 
-> 当s1或s2不为直接量时,s1 + s2 等价 s1.concat(s2),为new String(s1+s2)

trim():String    //去除字符串首部和末尾空格
native intern():String  //在常量池中(获取/生成)对应String,然后返回该引用

//4.3.3 字符串读取方法
valueOf(Object): //将基本数据类型转换为String类型
String s1 = input.next(); //读取字符串
String s2 = input.nextLine(); //读取一整行文本
int intValue = Integer.parseInt(intString); //数值字符串转Int类型
double doubleValue = Double.parseDouble(doubleString);
String s = number + ""; //数值转字符串类型

//4.3.4 格式化输出,底层用PrintStream.format(),使用Formatter类实现
System.out.format(“Interest is $%4.2f”,interest);
System.out.printf(“Interest is $%4.2f”,interest);

inner class

//为了代码复用 - 单例静态
private static class CaseInsensitiveComparator
        implements Comparator<String>, java.io.Serializable {
    // use serialVersionUID from JDK 1.2.2 for interoperability
    private static final long serialVersionUID = 8575799808933029326L;

    public int compare(String s1, String s2) {
        int n1 = s1.length();
        int n2 = s2.length();
        int min = Math.min(n1, n2);
        for (int i = 0; i < min; i++) {
            char c1 = s1.charAt(i);
            char c2 = s2.charAt(i);
            if (c1 != c2) {
                c1 = Character.toUpperCase(c1);
                c2 = Character.toUpperCase(c2);
                if (c1 != c2) {
                    c1 = Character.toLowerCase(c1);
                    c2 = Character.toLowerCase(c2);
                    if (c1 != c2) {
                        // No overflow because of numeric promotion
                        return c1 - c2;
                    }
                }
            }
        }
        return n1 - n2;
    }

    /** Replaces the de-serialized object. */
    private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
}

String特性

重载的操作符(‘+’)

  1. Java中只重载了String类的‘+’操作符,且不支持其他操作符的重载(语法糖
  2. String字符串使用‘+’操作符时,会先初始化一个StringBuilder对象,然后调用append()方法进行字符串的拼接。
  3. Java将源代码编译字节码文件时,会将多个非引用字符串拼接成一个字符串。
java:String str = "abc" + 1 + "bcd" + ab + "cdf" + "1";
class:String str = "abc1bcd" + ab + "cdf1";    //编译时优化

intern方法

  • new String(): 将在堆中创建一个String对象,并将引用指向堆
  • "直接赋值":将先从常量池中查找该String,存在则返回引用,不存在则在常量池中创建一个String对象,并返回引用

intern():

  • 如果常量池中已存在对应字符串,则返回常量池得引用,否则:
  • (JDK6之前,将字符串复制到常量池中,并返回常量池的引用
  • (JDK7之后,将在常量池中创建一个引用,该引用指向堆中的String对象,然后返回常量池中引用的值),后面即使用直接量创建字符串,也将指向该引用
// jdk7+
        String s1 = "1";
        String s2 = "2";
        String s3 = s1 + s2;
        String s4 = s1 + s2;
        String s7 = s3.intern();    //此时在常量池生成引用,指向堆内存
        String s8 = s4.intern();    //常量池中已存在字符串直接量"12",直接返回引用。
        String s5 = "1" + "2";
        //常量池中已存在字符串直接量"12",直接返回引用。


        System.out.println(s3 == s4);       //false
        System.out.println(s3 == s5);       //true
        System.out.println(s4 == s5);       //false
        System.out.println(s3 == s7);       //true
        System.out.println(s4 == s8);       //false

JAVA编码(char,code point,code unit)

unicode:(\u000000 - \u10FFFF)

为了统一所有的文字编码,unicode为每种语言中的每个字符设定了统一并且唯一的二进制编码,通常用两个字节表示一个字符,所以unicode每个平面可以组合出65535种不同的字符,一共17个平面。
- 基本多语言平面(Basic Multilingual Plane,BMP):(\u0000-\uffff)

char,code point,code unit

  • java中,String是使用unicode编码的,但是,一个char的范围只能表示BMP平面的字符,因此延申出了code point,code unit,
  • 当表示BMP平面中的字符时,只需一个code unit(char)
  • 当表示BMP平面以外的字符时,需要两个code unit来表示
  • Surrogates
    • 编号为 U+D800 至 U+DBFF 的规定为「High Surrogates」,共1024个。
    • 编号为 U+DC00 至 U+DFFF 的规定为「Low Surrogates」,共1024个。

猜你喜欢

转载自www.cnblogs.com/kiqi/p/12227912.html