• String类


    位置:java.lang.String

    预备知识:java中的代码点和代码单元

    一、基本介绍

    • String 类代表字符串。Java 程序中的所有字符串字面值都作为此类的实例实现。
    • 字符串是常量,它们的值在创建之后不能更改
    • 因为 String 对象是不可变的,所以可以共享
    • String类包括的方法可用于检查序列的单个字符、比较字符串、搜索字符串、提取子字符串、创建字符串副本并将所有字符全部转换为大写或小写(大小写映射基于 Character 类指定的 Unicode 标准版)
    • 提供处理 Unicode 代码点(即字符)和 Unicode 代码单元(即 char 值)的方法。 
    • Java 语言提供对字符串串联符号("+")以及将其他对象转换为字符串( toString() )的特殊支持。

    二、构造方法

    简单粗暴

    String A = "lalalalala";

    常用

    1. String(char[] value)  //以一个char数组初始化String
    2. String(char[] value, int offset, int count)  //以一个char数组的子数组初始化String(offset:子数组第一个字符的索引,count:子数组长度),越界抛出异常
    3. String(byte[] bytes)       //以一个byte数组初始化String
    4. String(byte[] bytes, int offset, int length)  //以一个byte数组的子数组初始化String (offset:子数组第一个字符的索引,count:子数组长度),越界抛出异常
    5. String(int[] codePoints, int offset, int count)  //以一个int数组的子数组初始化String(int数组存储Unicode代码点)
    6. String(String original)  //以一个String初始化String(构建一个现有字符串的副本)
    7. String(StringBuffer buffer)  //以StringBuffer初始化String
    8. String(StringBuilder builder)  //StringBuilder初始化String

    其他

    1. String(byte[] bytes, Charset charset)  //通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String
    2. String(byte[] bytes, int offset, int length, Charset charset)      //通过使用指定的 charset 解码指定的 byte 子数组,构造一个新的 String
    3. String(byte[] bytes, String charsetName)  //通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String
    4. String(byte[] bytes, int offset, int length, String charsetName)   //通过使用指定的字符集解码指定的 byte 子数组,构造一个新的 String

    三、常用方法

    自身有关方法(长度,某索引位置值)

    1. int length()  //返回此字符串的长度(字符串中Unicode代码单元数量)
    2. boolean isEmpty()       //当且仅当 length() 为 0 时返回 true
    3. char charAt(int index)   //返回指定索引处的 char 值(索引从0开始)
    4. int codePointAt(int index)   //返回指定索引处的字符Unicode代码点(索引值 0~length-1)
    5. int codePointBefore(int index)  //返回指定索引之前的字符Unicode代码点(索引值 1~length)
    6. int codePointCount(int beginIndex, int endIndex)  //返回此 String 的指定文本范围中的Unicode代码点数量(范围:beginIndex~endIndex-1)
    7. int offsetByCodePoints(int index, int codePointOffset)   //返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。

    比较方法

    1. boolean equals(Object anObject)     //将此字符串与指定的对象比较,当且仅当参数为String且与此对象相同时返回true
    2. boolean equalsIgnoreCase(String anotherString)    //将此 String 与另一个 String 比较,不考虑大小写。 
    3. int compareTo(String anotherString)       //按字典顺序比较两个字符串(anotherString在其之前返回正数,反之为负数,相同返回0)
    4. int compareToIgnoreCase(String str)        //按字典顺序比较两个字符串,不考虑大小写。 
    5. boolean contentEquals(CharSequence cs)      //将此字符串与指定的 CharSequence 比较。
    6. boolean contentEquals(StringBuffer sb)    //将此字符串与指定的 StringBuffer 比较。
    7. boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)       //测试两个字符串区域是否相等。 

    8. boolean regionMatches(int toffset, String other, int ooffset, int len)     //测试两个字符串区域是否相等。 

    检索方法

    1. boolean matches(String regex)   //告知此字符串是否匹配给定的正则表达式。 
    2. boolean contains(CharSequence s)     //当且仅当此字符串包含指定的 char 值序列时,返回 true。 
    3. boolean startsWith(String prefix)      //测试此字符串是否以指定的前缀开始。
    4. boolean startsWith(String prefix, int toffset)     //测试此字符串从指定索引开始的子字符串是否以指定前缀开始。 
    5. boolean endsWith(String suffix)   //测试此字符串是否以指定的后缀结束。 
    6. int indexOf(int ch)       //返回指定字符在此字符串中第一次出现处的索引。
    7. int indexOf(int ch, int fromIndex)      //返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
    8. int lastIndexOf(int ch)      //返回指定字符在此字符串中最后一次出现处的索引。
    9. int lastIndexOf(int ch, int fromIndex)   //返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
    10. int indexOf(String str)   //返回指定子字符串在此字符串中第一次出现处的索引。
    11. int indexOf(String str, int fromIndex)   //返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。 
    12. int lastIndexOf(String str)   //返回指定子字符串在此字符串中最右边出现处的索引。
    13. int lastIndexOf(String str, int fromIndex)     //返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。 

    提取子串方法

    1. String substring(int beginIndex)   //返回一个新的字符串,它是此字符串的一个子字符串。
    2. String substring(int beginIndex, int endIndex)   //返回一个新字符串,它是此字符串的一个子字符串。 
    3. CharSequence subSequence(int beginIndex, int endIndex)   //返回一个新的字符序列,它是此序列的一个子序列。
    4. String trim()   //返回字符串的副本,忽略前导空白和尾部空白。 

    拆分方法

    1. String[] split(String regex)  //根据给定正则表达式的匹配拆分此字符串。
    2. String[] split(String regex, int limit)  //根据匹配给定的正则表达式将此字符串拆分为至多limit份(若limit>0则从前往后最多拆分limit-1次,否则作用同无int参数版本)。 

    替换&大小写转换方法

    1. String replace(char oldChar, char newChar)  //返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
    2. String replace(CharSequence target, CharSequence replacement)       //使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
    3. String replaceFirst(String regex, String replacement)      //使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。 
    4. String replaceAll(String regex, String replacement)   //使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
    5. String toLowerCase()  //使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
    6. String toLowerCase(Locale locale)  //使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。 
    7. String toUpperCase()   //使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
    8. String toUpperCase(Locale locale)     //使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。 

    String转化为数组

    1. char[] toCharArray()    //将此字符串转换为一个新的字符数组。 
    2. byte[] getBytes()  //使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
    3. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)  //将字符从此字符串(的字串)复制到目标字符数组。 
    4. byte[] getBytes(Charset charset)        //使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。
    5. byte[] getBytes(String charsetName)   //使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

    数组转为String

    1. static String valueOf(char[] data)       //返回 char 数组参数的字符串表示形式。
    2. static String valueOf(char[] data, int offset, int count)     //返回 char 数组参数的特定子数组的字符串表示形式。
    3. static String copyValueOf(char[] data)     //返回指定数组中表示该字符序列的 String。
    4. static String copyValueOf(char[] data, int offset, int count)    //返回指定数组中表示该字符序列的 String
    5. static String format(Locale l, String format, Object... args)     //使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
    6. static String format(String format, Object... args)    //使用指定的格式字符串和参数返回一个格式化字符串。 

    将其他类型转为String

    1. String toString()  //返回此对象本身(它已经是一个字符串!)。
    2. static String valueOf(boolean b)        //返回 boolean 参数的字符串表示形式。
    3. static String valueOf(char c)       //返回 char 参数的字符串表示形式。
    4. static String valueOf(double d)  //返回 double 参数的字符串表示形式。
    5. static String valueOf(float f)       //返回 float 参数的字符串表示形式。
    6. static String valueOf(int i)  //返回 int 参数的字符串表示形式。
    7. static String valueOf(long l)        //返回 long 参数的字符串表示形式。
    8. static String valueOf(Object obj)       //返回 Object 参数的字符串表示形式。

    其他方法

    1. String concat(String str)     //将指定字符串连接到此字符串的结尾(使用'+'即可)。
    2. int hashCode()     //返回此字符串的哈希码。String intern()       //返回字符串对象的规范化表示形式(一个初始为空的字符串池,它由类 String 私有地维护。当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(用 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并返回此 String 对象的引用)。 

    JAVA API:https://docs.oracle.com/javase/7/docs/api/

  • 相关阅读:
    python中创建实例属性
    Python中if __name__ == "__main__": 的理解
    模块
    python函数式编程
    python-复杂生成式
    生成器的测试
    mysql乱码配置
    javascript
    Sql Server 2008 R2 下载地址
    Microsoft Data Access Components 2.8
  • 原文地址:https://www.cnblogs.com/Dumblidor/p/5520228.html
Copyright © 2020-2023  润新知