Thezmmm

Never do things by halves.

16 Jul 2022

JavaString

查找

  1. char charAt(int index)

    返回指定索引处的 char 值

  2. boolean contains(CharSequence chars)

    判断字符串中是否包含指定的字符或字符串

  3. int indexOf(int ch )

    int indexOf(int ch, int fromIndex)

    int indexOf(String str)

    int indexOf(String str, int fromIndex)

    • ch :字符,Unicode 编码。

    • fromIndex :开始搜索的索引位置,第一个字符是 0 ,第二个是 1 ,以此类推。

    • str :要搜索的子字符串

    返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1

  4. int lastIndexOf(int ch)

    int lastIndexOf(int ch, int fromIndex)

    int lastIndexOf(String str)

    int lastIndexOf(String str, int fromIndex)

    • ch :字符。
    • fromIndex :开始搜索的索引位置。
    • str :要搜索的子字符串。

    返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1

属性

  1. int length()

    返回字符串的长度

  2. boolean isEmpty()

    如果字符串为空返回 true,否则返回 false。

替换

  1. String replace(char searchChar, char newChar)

    替换后生成的新字符串

  2. String replaceAll(String regex, String replacement)

    • regex – 匹配此字符串的正则表达式。
    • replacement – 用来替换每个匹配项的字符串。
  3. String replaceFirst(String regex,String replacement)

    替换字符串第一个匹配给定的正则表达式的子字符串

拆分

  1. String[] split(String regex)

    String[] split(String regex, int limit)

    • regex – 正则表达式分隔符。
    • limit – 分割的份数。
  2. public String substring(int beginIndex) public String substring(int beginIndex, int endIndex)

    • beginIndex – 起始索引(包括), 索引从 0 开始。
    • endIndex – 结束索引(不包括)。

    返回一个新的字符串,它是此字符串的一个子字符串。

比较

  1. int compareTo(String str)

    按字典序,返回第一个不相同字符的ASCLL码的差值

  2. int compareToIgnoreCase(String str)

    按字典顺序比较两个字符串,不考虑大小写

  3. boolean equals(Object anObject)

    如果给定对象与字符串相等(指内容相同, == 比较的是地址是否相同),则返回 true;否则返回 false。

  4. boolean equalsIgnoreCase(String anotherString)

    比较内容是否相等,不考虑大小写

  5. boolean contentEquals(StringBuffer sb)

    字符串与指定 StringBuffer 表示相同的字符序列,则返回 true;否则返回 false

  6. boolean endsWith(String suffix)

    参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false

  7. boolean startsWith(String prefix)

    参数表示的字符序列是此对象表示的字符序列的前缀,则返回 true;否则返回 false

操作

  1. String concat(String s)

    将指定字符串连接到此字符串的结尾

  2. String toLowerCase()

    转换为小写的字符串

  3. String toUpperCase()

    转换为大写后的字符串

  4. String trim()

    删除字符串的头尾空白符

StringBuffer 和 StringBuilder

对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类

和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象

java-string-20201208.png (822×452) (runoob.com)

由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。

方法

  1. StringBuffer append(String s) 将指定的字符串追加到此字符序列。

  2. StringBuffer reverse() 将此字符序列用其反转形式取代。

  3. delete(int start, int end) 移除此序列的子字符串中的字符。

  4. insert(int offset, int i) 将 int 参数的字符串表示形式插入此序列中。

  5. insert(int offset, String str) 将 str 参数的字符串插入此序列中。

  6. replace(int start, int end, String str) 使用给定 String 中的字符替换此序列的子字符串中的字符。

  7. char charAt(int index) 返回此序列中指定索引处的 char 值。

  8. int indexOf(String str) 返回第一次出现的指定子字符串在该字符串中的索引。

    • int indexOf(String str, int fromIndex) 从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。
  9. int lastIndexOf(String str) 返回最右边出现的指定子字符串在此字符串中的索引。

    • int lastIndexOf(String str, int fromIndex) 返回 String 对象中子字符串最后出现的位置。
  10. int length()

返回长度(字符数)。
  1. void setCharAt(int index, char ch) 将给定索引处的字符设置为 ch

  2. CharSequence subSequence(int start, int end) 返回一个新的字符序列,该字符序列是此序列的子序列。

  3. String substring(int start) 返回一个新的 String,它包含此字符序列当前所包含的字符子序列。

    • String substring(int start, int end) 返回一个新的 String,它包含此序列当前所包含的字符子序列。
  4. String toString() 返回此序列中数据的字符串表示形式。

类型转换

  1. 字符数组 -> String

    1. copyValueOf

      static String copyValueOf(char[] data)

      static String copyValueOf(char[] data, int offset, int count)

      • offset : 子数组初始偏移量

      • count : 子数组的长度

      返回指定数组中表示该字符序列的字符串

    2. new String()

      new String(char [])

  2. String -> 字符数组

    • void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

      • srcBegin :字符串中要复制的第一个字符的索引。

      • srcEnd :字符串中要复制的最后一个字符之后的索引。

      • dst :目标数组。

      • dstBegin : 目标数组中的起始偏移量。

    • char[] toCharArray()

  3. ? -> String

    static String valueOf()

    返回给定类型参数的字符串表示形式