OpenJDK8 String类

String 字符串类型

String是不可变对象,创建后值(引用地址)不可变;StringBuffer、StringBuilder 支持可变字符串
提供了系列方法:单字符提取、字符串比较、字符串查询、字串提取、大小写字符串拷贝等

public final class String
implements java.io.Serializable, Comparable, CharSequence {

// 实际的字符存储,每个是一个char类型(Java中单个char为2字符)
private final char value[];

// 缓存字符串的hash值,默认值为0
private int hash;

private static final long serialVersionUID = -6849794470754667710L;

构造方法

/**
 * 新建一个空字符序列的String对象,由于String不可变,因此该构造函数没有什么具体意义
 */
public String() 

/**
 * 新建一个字符串对象,指向参数中的同一个字符序列。
 */
public String(String original)

/**
 * 创建一个新的String对象,并按字符数组中的数据赋值,深拷贝。对原数组的修改不影响本对象
 * @param  value
 *         The initial value of the string
 */
public String(char value[]) 

/**
 * 创建一个新的String对象,并按字符数组中的子数组[offset,offset+count)进行赋值,深拷贝。对原数组的修改不影响本对象
 * StringIndexOutOfBoundsException 索引越界异常 三种情况:
 *      (1)偏移量<0
 *      (2)截取长度<0 StringIndexOutOfBoundsException 索引越界异常
 *      (3)偏移量+长度>字符数组长度 StringIndexOutOfBoundsException 索引越界异常
 */
public String(char value[], int offset, int count)

/**
 * 创建一个新的String对象,并按#代码点#数组中的子数组[offset,offset+count)进行赋值,深拷贝。对原数组的修改不影响本对象
 * 代码点: java中某些字符(unicode大于0x10000的部分)得用两个char才能正常表示,代码点就能识别这样的字符,将它们视作一个字符进行处理,对用户透明
 * StringIndexOutOfBoundsException 索引越界异常 三种情况:
 *      (1)偏移量<0
 *      (2)截取长度<0 StringIndexOutOfBoundsException 索引越界异常
 *      (3)偏移量+长度>字符数组长度 StringIndexOutOfBoundsException 索引越界异常
 * IllegalArgumentException 非法参数异常:
 *      代码点数组中包含了无效的Unicode字符元素
 * @since  1.5
 */
public String(int[] codePoints, int offset, int count) 

/**
 * JDK1.1 过期的构造函数,不多做介绍
 */
@Deprecated
public String(byte ascii[], int hibyte, int offset, int count) 

/**
 * JDK1.1 过期的构造函数,不多做介绍
 */
@Deprecated
public String(byte ascii[], int hibyte) 

/**
 * 构建一个新的String对象,其值是字符子数组按特定字符解码后的结果,由于字符解码过程,新对象的长度与原子数组的长度可能不相等
 * NullPointerException 空指针异常: 未指定字符编码名称
 * UnsupportedEncodingException 不支持的编码异常
 * StringIndexOutOfBoundsException 索引越界异常 三种情况:
 *      (1)偏移量<0
 *      (2)截取长度<0 StringIndexOutOfBoundsException 索引越界异常
 *      (3)偏移量+长度>字符数组长度 StringIndexOutOfBoundsException 索引越界异常
 * @since  JDK1.1
 */
public String(byte bytes[], int offset, int length, String charsetName) throws UnsupportedEncodingException 

/**
 * 与之前方法类似,1.6提供了String的Charset字符集支持
 * @since  1.6
 */
public String(byte bytes[], int offset, int length, Charset charset)

/**
 * 对之前方法的封装
 * @since  JDK1.1
 */
public String(byte bytes[], String charsetName)   throws UnsupportedEncodingException 

/**
 * 对之前方法的封装
 * @since  1.6
 */
public String(byte bytes[], Charset charset)

/**
 * 与之前方法类似
 * @since  JDK1.1
 */
public String(byte bytes[], int offset, int length) 

/**
 * 对之前方法的封装
 * @since  JDK1.1
 */
public String(byte bytes[]) 

/**
 * 创建一个新的String对象,其值为StringBuffer中当前值的拷贝,深拷贝。对原对象的改变不影响本对象
 */
public String(StringBuffer buffer) 

/**
 * 创建一个新的String对象,其值为StringBuilder中当前值的拷贝,深拷贝。对原对象的改变不影响本对象
 * 更提供使用StringBuilder的ToString方法
 * @since  1.5
 */
public String(StringBuilder builder)

长度、判空、取字符

/**
 * 返回字符串的长度
 * 该长度等同于字符串中#代码点#的长度
 */
public int length()

/**
 * 当且仅当长度为0时,返回true
 * @since 1.6
 */
public boolean isEmpty() 

/**
 * 返回指定索引的字符(#代码点#),取值为[0,n-1]
 * StringIndexOutOfBoundsException 索引越界异常
 */
public char charAt(int index) 

jdk1.5后代码点相关方法

/**
 * 返回指定索引的#代码点#,取值为[0,n-1]
 * 如果指定的index为高位的char(部分unicode字符由两个char组成),会自动继续读取低位char。结合后才返回
 * StringIndexOutOfBoundsException 索引越界异常
 * @since      1.5
 */
public int codePointAt(int index)

/**
 * 返回指定索引的前一个#代码点#,索引取值为[1,n]
 * 如果指定的index的前一位置index-1为低位的char(部分unicode字符由两个char组成),且index-2非负,会自动继续读取高位char。结合后才返回
 * StringIndexOutOfBoundsException 索引越界异常
 * @since     1.5
 */
public int codePointBefore(int index) 

/**
 * 返回[beginIndex, endIndex)中的#代码点#数量
 * IndexOutOfBoundsException 索引越界异常
 * @since  1.5
 */
public int codePointCount(int beginIndex, int endIndex)

/**
 * 返回给定的index处偏移codePointOffset个#代码点#的索引
 * IndexOutOfBoundsException 索引越界异常
 * @since 1.5
 */
public int offsetByCodePoints(int index, int codePointOffset)

字符数组赋值

/**
 * Copy characters from this string into dst starting at dstBegin.
 * This method doesn't perform any range checking.
 */
void getChars(char dst[], int dstBegin)

/**
 * 将字符串的[srcBegin, srcEnd)内容填充进dst数组(从dstBegin索引开始)
 * IndexOutOfBoundsException 索引越界异常 分为以下5种情况:
 *      (1) srcBegin 为负
 *      (2) srcBegin > srcEnd
 *       (3) srcEnd 大于字符串长度
 *      (4) dstBegin 为负
 *      (5) dstBegin+(srcEnd-srcBegin) > dst数组长度
 */
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) 

/**
 * 过期方法
 */
@Deprecated
public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) 

/**
 * 按指定字符名编码字符串数据,以字节数组的形式返回
 * UnsupportedEncodingException 不支持的编码异常
 * @since  JDK1.1
 */
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException

/**
 * 按指定字符集编码字符串数据,以字节数组的形式返回
 * NullPointerException 空指针异常
 * @since  1.6
 */
public byte[] getBytes(Charset charset) 

/**
 * 默认字符编码String数据,以字节数组返回
 * @since      JDK1.1
 */
public byte[] getBytes() 

相等判断及大小比较

/**
 * 与给定的对象进行比较。
 * @see  #compareTo(String)
 * @see  #equalsIgnoreCase(String)
 */
public boolean equals(Object anObject)

/**
 * 与给定的StringBuffer比较,1.4版本接口,底层实现在1.5中已经做了优化
 * @since  1.4
 */
public boolean contentEquals(StringBuffer sb)

private boolean nonSyncContentEquals(AbstractStringBuilder sb)

/**
 * 与给定的对象进行值比较。给定对象为StringBuffer时,会使用synchronized加锁,避免并发问题
 * @since  1.5
 */
public boolean contentEquals(CharSequence cs) 

/**
 * 与另一个字符串对象比较,忽略大小写。
 * @see  #equals(Object)
 */
public boolean equalsIgnoreCase(String anotherString)

/**
 * 比较两个字符串对象
 * >0. 当前对象内容>给定的对象内容
 * =0. 两者相等
 * <0. 当前对象内容<给定的对象内容
 */
public int compareTo(String anotherString) 

/**
 * String对象的比较器
 * @since   1.2
 */
public static final Comparator<String> CASE_INSENSITIVE_ORDER  = new CaseInsensitiveComparator();
private static class CaseInsensitiveComparator implements Comparator<String>, java.io.Serializable 

/**
 * 比较两个String对象大小,忽略大小写
 * @since   1.2
 */
public int compareToIgnoreCase(String str) 

/**
 * 比较两个String对象中的子区域是否相等
 */
public boolean regionMatches(int toffset, String other, int ooffset, int len)

/**
 * 比较两个字符串的指定区间内容是否相等(支持忽略大小写)
 */
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 

/**
 * 判断当前String的toffset位置是否以prefix字符串开始
 */
public boolean startsWith(String prefix, int toffset) 

/**
 * 上面方法的封装
 * @since   1. 0
 */
public boolean startsWith(String prefix) {
    return startsWith(prefix, 0);
}

/**
 * 判断String是否以suffix为结尾。底层还是用startsWith实现
 */
public boolean endsWith(String suffix)

/**
 * 返回字符串的hashcode值,这里用了hash成员属性缓存
 */
public int hashCode() 

索引位置查询

/**
 * 返回String中某个字符第一次出现的索引位置
 */
public int indexOf(int ch)

/**
 * 返回String子串中[fromIndex, length)中第一次出现特定字符的索引位置,找不到返回-1
 */
public int indexOf(int ch, int fromIndex)

/**
 * Handles (rare) calls of indexOf with a supplementary character.
 */
private int indexOfSupplementary(int ch, int fromIndex) 

/**
 * 返回String中最后一次出现特定字符的索引
 */
public int lastIndexOf(int ch) {
    return lastIndexOf(ch, value.length - 1);
}

/**
 * 返回String子串中[fromIndex, length)中最后一次出现特定字符的索引位置,找不到返回-1
 */
public int lastIndexOf(int ch, int fromIndex)

/**
 * Handles (rare) calls of lastIndexOf with a supplementary character.
 */
private int lastIndexOfSupplementary(int ch, int fromIndex)

/**
 * 方法封装
 */
public int indexOf(String str) 

/**
 * 返回String中第一次出现特定字符串str的索引位置,找不到返回-1
 */
public int indexOf(String str, int fromIndex)

/**
 * 下面方法的封装
 */
public int lastIndexOf(String str) 

/**
 * 返回从fromIndex索引开始,最后一次出现str的索引位置。没出现,返回-1
 */
public int lastIndexOf(String str, int fromIndex) 

字符串截取、拼接、匹配、替换

/**
 * 截取字串。[beginIndex, length)
 */
public String substring(int beginIndex) 

/**
 * 截取字串。[beginIndex, endIndex)
 * StringIndexOutOfBoundsException 索引越界异常
 */
public String substring(int beginIndex, int endIndex)

/**
 * 截取String字串,并返回字符序列
 * @since 1.4
 * @spec JSR-51
 */
public CharSequence subSequence(int beginIndex, int endIndex)

/**
 * 字符串拼接,返回的是一个新的String对象
 */
public String concat(String str) 

/**
 * 替换String中的某个字符
 */
public String replace(char oldChar, char newChar) 

/**
 * 判断是否满足给定的正则式
 * @since 1.4
 * @spec JSR-51
 */
public boolean matches(String regex)

/**
 * 判断是否包含给定的字符序列,底层用indexOf方法实现
 * @since 1.5
 */
public boolean contains(CharSequence s)

/**
 * 替换第一个满足正则式的字符子串
 * @since 1.4
 * @spec JSR-51
 */
public String replaceFirst(String regex, String replacement)

/**
 * 替换所有满足正则式的字符子串
 * @since 1.4
 * @spec JSR-51
 */
public String replaceAll(String regex, String replacement)

/**
 * 字符串替换
 * @since 1.5
 */
public String replace(CharSequence target, CharSequence replacement)

/**
 * 按正则匹配分割字符串
 * @since 1.4
 * @spec JSR-51
 */
public String[] split(String regex, int limit)
/**
 * 上面方法的封装
 * @since 1.4
 * @spec JSR-51
 */
public String[] split(String regex) 

/**
 * 将字符串数组用特定的连接符拼接成一个新的String对象
 * 注意点: 遇到null会拼上"null"
 * @see java.util.StringJoiner
 * @since 1.8
 */
public static String join(CharSequence delimiter, CharSequence... elements)

/**
 * 将字符串数组用特定的连接符拼接成一个新的String对象
 * 注意点:如果元素中有null,会抛空指针异常
 * @see    java.util.StringJoiner
 * @since 1.8
 */
public static String join(CharSequence delimiter,  Iterable<? extends CharSequence> elements)

大小写转换

/**
 * 将所有字符转成小写
 * @since   1.1
 */
public String toLowerCase(Locale locale) 

/**
 * 上个方法的封装
 */
public String toLowerCase() 

/**
 * 将所有字符转成大写
 * @since   1.1
 */
public String toUpperCase(Locale locale)

/**
 * 上个方法的封装
 * @see     java.lang.String#toUpperCase(Locale)
 */
public String toUpperCase() 

格式化

/**
 * 去除首尾的空格
 */
public String trim() 

/**
 * toString方法返回自身
 */
public String toString()

/**
 * 转成字符数组
 */
public char[] toCharArray()

/**
 * 返回一个格式化的字符串
 * IllegalFormatException 非法的格式化异常
 * @see  java.util.Formatter
 * @since  1.5
 */
public static String format(String format, Object... args) 

/**
 * 按给定的方言,格式化和参数数组,返回一个格式化的字符串
 * @see  java.util.Formatter
 * @since  1.5
 */
public static String format(Locale l, String format, Object... args) 

/**
 * 对象/基本类型 to String
 */
public static String valueOf(Object obj)
public static String valueOf(char data[]) 
public static String valueOf(char data[], int offset, int count)
public static String copyValueOf(char data[], int offset, int count)
public static String copyValueOf(char data[]) 
public static String valueOf(boolean b) 
public static String valueOf(char c) 
public static String valueOf(int i) 
public static String valueOf(long l) 
public static String valueOf(float f) 
public static String valueOf(double d) 

intern方法

/**
 * 返回规范化表示的String对象
 * 调用intern方法时,会先在常量池中查找equals为true的字符串常量。
 *      1)如果存在,会直接返回常量池中的对象;
 *      2)如果不存在,会将string添加进常量池,并返回该对象的引用
 *  所有的文本字符串和string类型的常量表达式默认都是intern的
 */
public native String intern();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值