一、String类
- String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
- 字符串是一个特殊的对象
- 字符串是常量,一旦初始化就不可以被改变
- String str = “abc”;和String str1 = new String(“abc”);的区别在于:
- str==str1----false str.equals(str1)----true
- str在内存中有一个对象“abc”
- str1在内存中有两对象“abc”和new String
- String类重写了Object类中的equals方法,该方法用于判断字符串内容是否相等
- “abc”是一个常量,存放于方法区的常量池
二、常用方法
- /**
- *
- */
- public final class String
- implements java.io.Serializable, Comparable<String>, CharSequence
- {
- private final char value[];//字符数组
- private final int offset;
- private final int count;//length
- private int hash;
- private static final long serialVersionUID = -6849794470754667710L;
- //构造函数
- //初始化一个新创建的 String 对象,使其表示一个空字符序列。
- public String() {
- this.offset = 0;
- this.count = 0;
- this.value = new char[0];
- }
- //初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。
- public String(String original) {
- int size = original.count;
- char[] originalValue = original.value;
- char[] v;
- if (originalValue.length > size) {
- int off = original.offset;
- v = Arrays.copyOfRange(originalValue, off, off+size);
- } else {
- v = originalValue;
- }
- this.offset = 0;
- this.count = size;
- this.value = v;
- }
- //分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
- public String(char value[]) {
- this.offset = 0;
- this.count = value.length;
- this.value = StringValue.from(value);
- }
- //分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
- public String(char value[], int offset, int count) {
- if (offset < 0) {
- throw new StringIndexOutOfBoundsException(offset);
- }
- if (count < 0) {
- throw new StringIndexOutOfBoundsException(count);
- }
- if (offset > value.length - count) {
- throw new StringIndexOutOfBoundsException(offset + count);
- }
- this.offset = 0;
- this.count = count;
- this.value = Arrays.copyOfRange(value, offset, offset+count);
- }
- //通过使用指定的字符集解码指定的 byte 子数组,构造一个新的 String。
- public String(byte bytes[], int offset, int length, String charsetName)throws UnsupportedEncodingException
- {
- if (charsetName == null)
- throw new NullPointerException("charsetName");
- checkBounds(bytes, offset, length);
- char[] v = StringCoding.decode(charsetName, bytes, offset, length);
- this.offset = 0;
- this.count = v.length;
- this.value = v;
- }
- //通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
- public String(byte bytes[], String charsetName)throws UnsupportedEncodingException
- {
- this(bytes, 0, bytes.length, charsetName);
- }
- public String(byte bytes[], int offset, int length) {
- }
- public String(byte bytes[]) {
- this(bytes, 0, bytes.length);
- }
- public String(StringBuffer buffer) {
- String result = buffer.toString();
- this.value = result.value;
- this.count = result.count;
- this.offset = result.offset;
- }
- public String(StringBuilder builder) {
- String result = builder.toString();
- this.value = result.value;
- this.count = result.count;
- this.offset = result.offset;
- }
- //获取方法************************************
- //1,返回此字符串的长度
- public int length() {
- return count;
- }
- //2,返回指定索引处的 char 值
- public char charAt(int index) {
- if ((index < 0) || (index >= count)) {
- throw new StringIndexOutOfBoundsException(index);
- }
- return value[index + offset];
- }
- //3,返回指定字符在此字符串中第一次出现处的索引。
- public int indexOf(int ch) {
- return indexOf(ch, 0);
- }
- //4,返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
- public int indexOf(int ch, int fromIndex) {
- //....
- }
- //5,返回指定子字符串在此字符串中第一次出现处的索引
- public int indexOf(String str)
- //6,返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
- public int indexOf(String str, int fromIndex)
- //7,返回指定字符在此字符串中最后一次出现处的索引。
- public int lastIndexOf(int ch)
- //8,返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
- public int lastIndexOf(int ch, int fromIndex)
- //9,返回指定子字符串在此字符串中最右边出现处的索引
- public int lastIndexOf(String str)
- //10,返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
- public int lastIndexOf(String str, int fromIndex)
- /**
- *所有查找字符或字符串位置的如果没有,则返回-1
- *charAt函数中的index如果超过字符串的长度则会出现字符串角标越界异常IndexOutOfBoundsException是RuntimeException的子类
- */
- }
- {
- //判断方法*****************************************
- //测试此字符串是否以指定的前缀开始。
- public boolean startsWith(String prefix)
- //测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
- public boolean startsWith(String prefix, int toffset)
- //测试此字符串是否以指定的后缀结束。
- public boolean endsWith(String suffix)
- //当且仅当 length() 为 0 时返回 true。 即可以判断字符串是否为空
- public boolean isEmpty()
- //当且仅当此字符串包含指定的 char 值序列时,返回 true。 即可以判断字符串中是否包含指定字符串,也可以用indexOf(String str)来判断
- //接口 java.lang.CharSequence
- //所有已知子接口:Name
- //所有已知实现类:CharBuffer, Segment, String, StringBuffer, StringBuilder
- public boolean contains(CharSequence s)
- //判断两个字符串内容是否相同
- public boolean equals(Object anObject)
- //将此 String 与另一个 String 比较,不考虑大小写
- public boolean equalsIgnoreCase(String anotherString)
- }
- {
- //转换方法***********
- //1,将字符数组,转换成字符串
- public String(char[] value)
- 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。该字符数组的内容已被复制;后续对字符数组的修改不会影响新创建的字符串-----将字符数组,转换成字符串
- public String(char[] value, int offset,int count)
- //2,将字符数组的一部分,转换成字符串offset是起始位置,count是长度
- public static String copyValueOf(char[] data)
- //3,返回指定数组中表示该字符序列的 String。
- public static String copyValueOf(char[] data, int offset, int count)
- //4,返回指定数组中表示该字符序列的 String
- public static String valueOf(char[] data,int offset, int count)
- public static String valueOf(char[] data)
- //5,将字符串转换成字符数组
- public char[] toCharArray()
- //6,将字节数组转换成字符串
- public String(byte[] bytes)
- public String(byte[] bytes, int offset,int length)
- //7,将字符串转换成字节数组,使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
- public byte[] getBytes()
- //8,使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
- public byte[] getBytes(String charsetName)
- //9,将基本数据类型转换成字符串
- public static String valueOf(int i)。。。。。
- 注意:字符串和字节数组在转换过程中,是可以指定编码表的
- }
- {
- //替换方法*********************
- //返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
- public String replace(char oldChar,char newChar)
- //用新字符串替换原有字符串,如果要替换的部分不存在就返回原字符串
- public String replace(CharSequence target,CharSequence replacement)
- //使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串
- public String replaceAll(String regex, String replacement)
- }
- //切割方法********************
- //根据给定规则的匹配拆分此字符串。
- public String[] split(String regex)
- //获取字符串中子串*******************
- //返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。
- public String substring(int beginIndex)
- //包含头不包含尾
- public String substring(int beginIndex,int endIndex)
- //转换,去除空格,比较*******************
- //1,将字符串转换成大写小写
- public String toLowerCase()//转成小写
- public String toUpperCase()//转成大写
- //2,将字符串两端多余的空格去除
- public String trim()
- //3,对两个字符串进行自然顺序的比较
- public int compareTo(String anotherString)
- 大于就返回正数
- 小于返回负数
- 等于返回0
- //比较时不考虑大小写
- public int compareToIgnoreCase(String str)