字符串常用的API
返回指定索引处的 char 值。
public char charAt(int index) { }
例如:
String i = "abcdefg";
char at = i.charAt(2);
System.out.println(at); // c
返回一个字符串,其值是该字符串重复 count 次的串联。
public String repeat(int count) { }
例如:
String str3 = "abc";
System.out.println(str3.repeat(3)); // abcabcabc
-
getBytes系列的方法:
使用平台的默认字符集将此 String 编码为字节序列,并将结果存储到新的字节数组中。 public byte[] getBytes() { } // 例如 // 将字符串转换为字节: String name = "ZhangSan"; byte[] bytes = name.getBytes(); System.out.println(Arrays.toString(bytes)); //[90, 104, 97, 110, 103, 83, 97, 110] // 使用指定的字符集将此 String 编码为字节序列,并将结果存储到新的字节数组中。 public byte[] getBytes(Charset charset) {} public byte[] getBytes(String charsetName){} // 将此字符串中的字符复制到目标字符数组中。 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { }
-
判断字符串是否为空的方法:
-
public boolean isBlank() { } 如果字符串为空或仅包含空格代码点,则返回 true,否则返回 false。 public boolean isEmpty() { } 当且仅当 length() 为 0 时返回 true。
区别:isEmpty() 方法 和 isBlank() 方法
isEmpty() 方法是在 Java 6 中引入的,用于检查字符串是否为空。具体行为如下:
如果字符串的长度为0,即它不包含任何字符,那么 isEmpty() 返回 true。
如果字符串为 null,则调用 isEmpty() 会引发 NullPointerException 异常。
如果字符串包含空格或制表符等空白字符,isEmpty() 仍然返回 false,因为它们被视为非空字符。
isBlank() 方法是在 Java 11 中引入的,用于检查字符串是否为空或仅由空白字符组成。具体行为如下:
如果字符串的长度为0,即它不包含任何字符,或者字符串仅包含空白字符(空格、制表符、换行符等),那么 isBlank() 返回 true。
-
查找指定字符或子串在此字符串中第一次出现的方法:
-
public int indexOf(int ch) { } 返回指定字符在此字符串中第一次出现的索引。 public int indexOf(int ch, int fromIndex) { } 返回此字符串中指定字符第一次出现的索引,从指定索引处开始搜索。 public int indexOf(String str) { } public int indexOf(String str, int fromIndex){ } 返回此字符串中指定子字符串第一次出现的索引。
-
返回此字符串中最后一次出现的指定字符的索引:
-
public int lastIndexOf(int ch){ } public int lastIndexOf(int ch, int fromIndex) public int lastIndexOf(String str) public int lastIndexOf(String str, int fromIndex)
-
替换子串的方法:
-
返回用 newChar 替换该字符串中所有出现的 oldChar 所得到的字符串。 public String replace(char oldChar, char newChar) 例如: String h = "a,a"; System.out.println(h.replace('a', 'b')); // b,b 将此字符串中与目标序列匹配的每个子字符串替换为指定的替换序列。 public String replace(CharSequence target, CharSequence replacement) { } 例如: String h = "a,a"; System.out.println(h.replace("a","bc")); // bc,bc 将此字符串中与给定正则表达式匹配的每个子字符串替换为给定替换。 public String replaceAll(String regex, String replacement){ } 将此字符串中与给定正则表达式匹配的第一个子字符串替换为给定替换。 public String replaceFirst(String regex, String replacement) { }
-
进行字符串比较的方法:
-
按字典顺序比较两个字符串。 public int compareTo(String anotherString) 按字典顺序比较两个字符串,忽略大小写差异。 public int compareToIgnoreCase(String str) 将此字符串与指定对象进行比较。 public boolean equals(Object anObject) { } 将此字符串与另一个字符串进行比较,忽略大小写。 public boolean equalsIgnoreCase(String anotherString) { } // 注意: // 空串 != null String f = ""; String g = null; System.out.println(f == g); // false // System.out.println(s.length()); // NullPointerException
-
拼接字符串的方法:
-
public String concat(String str) { } 将指定字符串连接到该字符串的末尾。
-
判断是否包含某个字符序列的方法:
-
public boolean contains(CharSequence s) { } 当且仅当此字符串包含指定的 char 值序列时返回 true。
public static String copyValueOf(char data[], int offset, int count) { }
就相当于valueOf(char[], int, int).
注意这是一个静态方法。
-
判断字符串是否以指定字符开头或结尾:
-
public boolean endsWith(String suffix) { } 测试该字符串是否以指定后缀结尾。 public boolean startsWith(String prefix) 测试此字符串是否以指定前缀开头。 public boolean startsWith(String prefix, int toffset) { } 测试此字符串中从指定索引开始的子字符串是否以指定前缀开头。
-
格式化字符串的方法:
-
public static String format(String format, Object... args) { } 使用指定的格式字符串和参数返回格式化字符串。 public static String format(Locale l, String format, Object... args) { } 使用指定的区域设置、格式字符串和参数返回格式化字符串。 public String formatted(Object... args) { } 使用此字符串作为格式字符串和提供的参数进行格式化。 public String indent(int n) { } 根据n的值调整该字符串每行的缩进,并标准化行终止符。
-
public native String intern();
-
intern如果字符串在常量池中有就返回常量池中的对象,如果没有就将该对象放入常量池中,并将这个对象返回
-
-
public boolean matches(String regex){ }
-
判断该字符串是否与给定的正则表达式匹配。
-
-
字符串拆分的方法:
-
返回一个新字符串,该字符串由 CharSequence 元素的副本与指定分隔符的副本连接在一起组成。 public static String join(CharSequence delimiter,CharSequence... elements){ } 例如: // 字符串的合并【static方法】 // String h = String.join(",", "a", "abc","-", " ","a"); String h = String.join(",", "a","a"); System.out.println(h); // a,a System.out.println(h.length()); // 3 围绕给定正则表达式的匹配项拆分此字符串 public String[] split(String regex){}
-
删除各种空格的方法:
-
public String strip() 返回一个字符串,其值为该字符串,并删除所有前导和尾随空格。 public String stripLeading() 返回一个字符串,其值为该字符串,并删除所有前导空格。 public String stripTrailing() 返回一个字符串,其值为该字符串,并删除所有尾随空格。 public String trim(){ }
-
截取子串的方法:
-
public CharSequence subSequence(int beginIndex, int endIndex) { } 返回作为该序列的子序列的字符序列。 public String substring(int beginIndex) 返回一个字符串,该字符串是该字符串的子字符串。 substring(int beginIndex, int endIndex){} 例如: "hamburger".substring(4, 8) returns "urge" "smiles".substring(1, 5) returns "mile" public char[] toCharArray() { 将此字符串转换为新的字符数组。
-
大小写转换的方法:
-
String toLowerCase() // 全部转换为小写字母 toLowerCase(Locale locale) toUpperCase() // 全部转换为大写字母 toUpperCase(Locale locale)
-
将其它数据类型的数据转换为字符串的表示形式:
-
注意都是static静态方法
-
static String valueOf(boolean b){} // 将布尔类型的转换为字符串 eg: String valueOfIntType = String.valueOf(5); System.out.println(valueOfIntType); // 5 static String valueOf(char c){} // 将char类型的字符转换为字符串 eg: String charType = String.valueOf('q'); System.out.println(charType); // q static String valueOf(char[] data){} //将char类型的数组转换为字符串 eg: String charArrayType = String.valueOf(new char[]{'a', 'b', 'c'}); System.out.println(charArrayType); //abc static String valueOf(char[] data, int offset, int count){} // 将char数组中的特定范围内的字符转换为字符串 eg: String valued = String.valueOf(new char[]{'a', 'b', 'c', 'd', 'e'}, 2, 3); System.out.println(valued); // cde static String valueOf(float f){} // 将浮点型转换为字符串 static String valueOf(int i){} // 将int类型转换为字符串 static String valueOf(double d){} // 将double类型转换为字符串 static String valueOf(long l){} // 将long类类型转换为字符串 static String valueOf(Object obj){} // 将引用类型转换为字符串 eg: String obj = String.valueOf(new Person()); System.out.println(obj); //Person{name='null', gender=null, age=null, marry=null}
-
获取指定索引位置处的字符的Unicode码点(code point):
public int codePointAt(int index){ }
/*
codePointAt 方法是Java中字符串类 String 的一个方法,它用于获取指定索引位置处的字符的Unicode码点(code point)。Unicode码点是表示字符的整数值,用于支持各种字符集和国际化字符。
*/
String text = "Hello, 世界!"; // 包含英文和中文字符
// 获取第一个字符(H)的Unicode码点
int codePoint1 = text.codePointAt(0);
System.out.println("Unicode码点1: " + codePoint1); // 输出:Unicode码点1: 72
// 获取第一个中文字符(世)的Unicode码点
int codePoint2 = text.codePointAt(7);
System.out.println("Unicode码点2: " + codePoint2); // 输出:Unicode码点2: 19990