📜  Java Java类设置 2

📅  最后修改于: 2022-05-13 01:55:10.793000             🧑  作者: Mango

Java Java类设置 2

Java Java类Set 1 在本文中,我们将讨论Java.lang.String 提供的不同构造函数和方法。 Java中的字符串是不可变的。现在让我们讨论 String 类提供的一些方法。方法:

  1. public int codePointAt(int index) – 它将索引作为参数,该索引必须从 0 到 length() – 1。ad 返回索引的字符unicode 点
  2. public int codePointBefore(int index) – 它将一个索引作为参数,该索引必须从 0 到 length() – 1。并返回该索引之前字符的unicode 点
  3. public int codePointCount(int start_index, int end_index) – 它以参数start_indexend_index为参数,返回范围之间的Unicode 代码点的计数。
  4. public CharSequence subSequence(int start_index, int end_index) – 此方法返回CharSequence ,它是调用此方法的字符串的子序列。注意:它的行为类似于subString(int start_index, int end_index) ,但subString()返回StringsubSequence返回CharSequence。
  5. public boolean contains(CharSequence char_seq) - 如果给定的CharSquence存在于调用它的字符串中,则返回true
  6. public boolean contentEquals(CharSequence char_seq) – 仅当给定的CharSequence与调用它的字符串完全匹配时才返回true
  7. public boolean endsWith(String suf) - 它接受参数一个字符串后缀,如果字符串具有相同的后缀,则返回true
  8. public boolean startsWith(String pre) – 它接受参数一个字符串前缀,如果字符串具有相同的前缀,则返回true
  9. public void getChars(int start, int end, char[] destination, int destination_start) :有四个参数, start和end是要复制到字符数组的范围, destination是要复制到的字符数组, destination_start是目标数组的起始位置。
  10. public char[] toCharArray() – 将整个字符串转换为字符数组。注意:- getChars提供了更大的灵活性,当将一系列字符复制到现有数组或新数组时,而toCharArray将整个字符串转换为新字符数组。
  11. public int hashCode() - 它返回给定字符串的哈希码。有一个预定义的公式来计算字符串的哈希码:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
where,
n - is the length of the String
i - is the ith character of the string
  1. public String intern() – 它返回调用它的 String 对象的规范形式。 ” 当调用 intern 方法时,如果池中已经包含一个与该 String 对象相等的字符串,由 equals(Object) 方法确定,则返回池中的字符串。否则,将此 String 对象添加到池中并返回对该 String 对象的引用。 ” Java字符串文档。
  2. public boolean isEmpty() – 如果 String 的长度0 ,则返回 true。
  3. public static String format(String f, Object... arguments) - 根据格式说明符f返回格式化的字符串,参数应该完全等于使用的格式说明符的数量。变化: public static String format(Locale l, String f, Object... arguments) – 根据使用的Locale返回格式化的字符串。
  4. public boolean matches(String reg_exp) – 如果字符串匹配正则表达式 (reg_exp),则返回 true。
  5. public boolean regionMatches(int start_OString, String another, int start_AString, int no_of_char) - 如果以索引start_OString开头的原始字符串的区域与以string_AString开头的另一个字符串的区域匹配,则返回 true ,并且no_of_char是指要转换的字符数进行比较。变体: public boolean regionMatches(boolean ignore_case, int start_OString, String another, int start_AString, int no_of_char) - 当我们想在比较子字符串时忽略大小写时,这种方法的变体提供了灵活性。如果第一个参数,即ignore_case,则忽略大小写并进行比较,但如果为,则其行为类似于没有ignore_case的方法的第一个版本
  6. public String[] split(String reg_exp) – 它围绕正则表达式拆分字符串并返回一个字符串数组。变体: public String[] split(String reg_exp, int limit) - 它围绕正则表达式拆分字符串, limit指的是应用reg_exp的次数,它是结果数组的长度,应用 reg_exp 为 n只有长度 - 1 次。
  7. public static String join(CharSequence de_limiter, CharSequence... elements) – 它返回一个字符串,其中包含由de_limiter连接的所有元素变体: public static String join(CharSequence de_limiter, Iterable elements) - 它执行相同的函数,但第二个参数是Iterable ,这使得它可以灵活地处理不同的集合类。
  8. public String replaceAll(String reg_exp, String replacement) - 用replacement替换原始字符串中所有匹配reg_exp的子字符串,并返回修改后的字符串。
  9. public String replaceFirst(String reg_exp, String replacement) - 它用替换替换原始字符串中一次出现的正则表达式,并返回修改后的字符串。注意:- replaceAllreplaceFirst不会更改原始字符串,而是创建一个经过修改的新字符串。