Java - String 类
字符串(Strings)在 Java 编程中被广泛使用,是一个字符序列。在 Java 编程语言中,字符串被视为对象。
Java 平台提供了 String 类 来创建和操作字符串。
创建字符串
创建字符串的最直接方式是编写 −
String greeting = "Hello world!";
每当编译器在你的代码中遇到字符串字面量时,就会创建一个 String 对象,其值为“Hello world!”。
与其他对象一样,你可以使用 new 关键字和构造函数来创建 String 对象。String 类有 11 个构造函数,允许你使用不同的来源(如字符数组)提供字符串的初始值。
Java 中创建字符串的示例
public class StringDemo {
public static void main(String args[]) {
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String helloString = new String(helloArray);
System.out.println( helloString );
}
}
输出
hello.
注意 − String 类是不可变的,因此一旦创建,String 对象就不能被改变。如果需要对字符字符串进行大量修改,则应使用 StringBuffer 和 StringBuilder 类。
字符串长度
用于获取对象信息的方方法被称为 访问器方法(accessor methods)。你可以使用的一个字符串访问器方法是 length() 方法,它返回字符串对象中包含的字符数。
以下程序是 length() 方法 String 类的示例。
Java 中获取字符串长度的示例
public class StringDemo {
public static void main(String args[]) {
String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
System.out.println( "String Length is : " + len );
}
}
这将产生以下结果 −
输出
String Length is : 17
连接字符串
String 类包含一个用于连接两个字符串的方法 −
string1.concat(string2);
这会返回一个新字符串,即 string1 末尾添加了 string2。你也可以将 concat() 方法与字符串字面量一起使用,如 −
"My name is ".concat("Zara");
字符串更常使用 + 操作符进行连接,如 −
"Hello," + " world" + "!"
结果为 −
"Hello, world!"
让我们看以下示例 −
Java 中连接字符串的示例
public class StringDemo {
public static void main(String args[]) {
String string1 = "saw I was ";
System.out.println("Dot " + string1 + "Tod");
}
}
这将产生以下结果 −
输出
Dot saw I was Tod
创建格式化字符串
你可以使用 printf() 和 format() 方法以格式化数字打印输出。String 类有一个等效的类方法 format(),它返回 String 对象而不是 PrintStream 对象。
使用 String 的静态 format() 方法可以创建一个可重用的格式化字符串,而不是一次性打印语句。例如,与其使用 −
Java 中创建格式化字符串的示例
System.out.printf("The value of the float variable is " +
"%f, while the value of the integer " +
"variable is %d, and the string " +
"is %s", floatVar, intVar, stringVar);
你也可以编写 −
String fs;
fs = String.format("The value of the float variable is " +
"%f, while the value of the integer " +
"variable is %d, and the string " +
"is %s", floatVar, intVar, stringVar);
System.out.println(fs);
Java String 类方法
以下是 Java 中的 String 类 的内置方法及其语法和用法 −
| 序号 | 方法 & 描述 |
|---|---|
| 1 | char charAt(int index)
此方法返回指定索引处的 char 值。 |
| 2 | int codePointAt(int index)
此方法返回指定索引处的字符(Unicode 代码点)。 |
| 3 | int codePointBefore(int index)
此方法返回指定索引之前的字符(Unicode 代码点)。 |
| 4 | int codePointCount(int beginIndex, int endIndex)
此方法返回此 String 指定文本范围内的 Unicode 代码点数量。 |
| 5 | int compareTo(String anotherString)
此方法按字典顺序比较两个字符串。 |
| 6 | int compareToIgnoreCase(String str)
此方法按字典顺序比较两个字符串,忽略大小写差异。 |
| 7 | String concat(String str)
此方法将指定的字符串连接到此字符串的末尾。 |
| 8 | boolean contains(CharSequence s)
此方法仅当此字符串包含指定的 char 值序列时返回 true。 |
| 9 | boolean contentEquals(CharSequence cs)
此方法将此字符串与指定的 CharSequence 进行比较。 |
| 10 | boolean contentEquals(StringBuffer sb)
此方法将此字符串与指定的 StringBuffer 进行比较。 |
| 11 | static String copyValueOf(char[] data)
此方法返回表示指定数组中字符序列的 String。 |
| 12 | static String copyValueOf(char[] data, int offset, int count)
此方法返回表示指定数组中字符序列的 String。 |
| 13 | boolean endsWith(String suffix)
此方法测试此字符串是否以指定的后缀结束。 |
| 14 | boolean equals(Object anObject)
此方法将此字符串与指定的对象进行比较。 |
| 15 | boolean equalsIgnoreCase(String anotherString)
此方法将此 String 与另一个 String 进行比较,忽略大小写。 |
| 16 | static String format(Locale l, String format, Object... args)
此方法使用指定的 locale、格式字符串和参数返回格式化的字符串。 |
| 17 | static String format(String format, Object... args)
此方法使用指定的格式字符串和参数返回格式化的字符串。 |
| 18 | byte[] getBytes()
此方法使用平台的默认 charset 将此 String 编码为字节序列,并将结果存储到新的 byte 数组中。 |
| 19 | byte[] getBytes(Charset charset)
此方法使用给定的 charset 将此 String 编码为字节序列,并将结果存储到新的 byte 数组中。 |
| 20 | byte[] getBytes(String charsetName)
此方法使用指定的 charset 将此 String 编码为字节序列,并将结果存储到新的 byte 数组中。 |
| 21 | void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
此方法将此字符串中的字符复制到目标字符数组中。 |
| 22 | int hashCode()
此方法返回此字符串的 hash code。 |
| 23 | int indexOf(int ch)
此方法返回此字符串中指定字符首次出现的位置索引。 |
| 24 | int indexOf(int ch, int fromIndex)
此方法返回此字符串中指定字符首次出现的位置索引,从指定的索引开始搜索。 |
| 25 | int indexOf(String str)
此方法返回此字符串中指定子字符串首次出现的位置索引。 |
| 26 | int indexOf(String str, int fromIndex)
此方法返回此字符串中指定子字符串首次出现的位置索引,从指定的索引开始。 |
| 27 | String intern()
此方法返回字符串对象的规范表示形式。 |
| 28 | boolean isEmpty()
此方法仅当 length() 为 0 时返回 true。 |
| 29 | int lastIndexOf(int ch)
此方法返回此字符串中指定字符最后出现的位置索引。 |
| 30 | int lastIndexOf(int ch, int fromIndex)
此方法返回此字符串中指定字符最后出现的位置索引,从指定的索引开始向后搜索。 |
| 31 | int lastIndexOf(String str)
此方法返回此字符串中指定子字符串最右边出现的位置索引。 |
| 32 | int lastIndexOf(String str, int fromIndex)
此方法返回此字符串中指定子字符串最后出现的位置索引,从指定的索引开始向后搜索。 |
| 33 | int length()
此方法返回此字符串的长度。 |
| 34 | boolean matches(String regex)
此方法指示此字符串是否匹配给定的正则表达式。 |
| 35 | int offsetByCodePoints(int index, int codePointOffset)
此方法返回此 String 中从给定索引偏移 codePointOffset 个代码点后的索引。 |
| 36 | boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
此方法测试两个字符串区域是否相等,忽略大小写。 |
| 37 | boolean regionMatches(int toffset, String other, int ooffset, int len)
此方法测试两个字符串区域是否相等。 |
| 38 | String replace(char oldChar, char newChar)
此方法返回一个新字符串,将此字符串中所有 oldChar 替换为 newChar。 |
| 39 | String replace(CharSequence target, CharSequence replacement)
此方法将此字符串中匹配字面 target 序列的每个子字符串替换为指定的字面 replacement 序列。 |
| 40 | String replaceAll(String regex, String replacement)
此方法将此字符串中匹配给定正则表达式的每个子字符串替换为给定的 replacement。 |
| 41 | String replaceFirst(String regex, String replacement)
此方法将此字符串中匹配给定正则表达式的第一个子字符串替换为给定的 replacement。 |
| 42 | String[] split(String regex)
此方法围绕给定正则表达式的匹配将此字符串拆分。 |
| 43 | String[] split(String regex, int limit)
此方法围绕给定正则表达式的匹配将此字符串拆分。 |
| 44 | boolean startsWith(String prefix)
此方法测试此字符串是否以指定的前缀开头。 |
| 45 | boolean startsWith(String prefix, int toffset)
此方法测试此字符串从指定索引开始的子字符串是否以指定的前缀开头。 |
| 46 | CharSequence subSequence(int beginIndex, int endIndex)
此方法返回此序列的子序列,即新的字符序列。 |
| 47 | String substring(int beginIndex)
此方法返回此字符串的子字符串,即新字符串。 |
| 48 | String substring(int beginIndex, int endIndex)
此方法返回此字符串的子字符串,即新字符串。 |
| 49 | char[] toCharArray()
此方法将此字符串转换为新的字符数组。 |
| 50 | String toLowerCase()
此方法使用默认 locale 的规则将此 String 中的所有字符转换为小写。 |
| 51 | String toLowerCase(Locale locale)
此方法使用给定 Locale 的规则将此 String 中的所有字符转换为小写。 |
| 52 | String toString()
此方法返回字符串本身。 |
| 53 | String toUpperCase()
此方法使用默认 locale 的规则将此 String 中的所有字符转换为大写。 |
| 54 | String toUpperCase(Locale locale)
此方法使用给定 Locale 的规则将此 String 中的所有字符转换为大写。 |
| 55 | String trim()
此方法返回字符串的副本,去除首尾空白字符。 |
| 56 | static String valueOf(boolean b)
此方法返回 boolean 参数的字符串表示形式。 |
| 57 | static String valueOf(char c)
此方法返回 char 参数的字符串表示形式。 |
| 58 | static String valueOf(char[] data)
此方法返回 char 数组参数的字符串表示形式。 |
| 59 | static String valueOf(char[] data, int offset, int count)
此方法返回 char 数组参数特定子数组的字符串表示形式。 |
| 60 | static String valueOf(double d)
此方法返回 double 参数的字符串表示形式。 |
| 61 | static String valueOf(float f)
此方法返回 float 参数的字符串表示形式。 |
| 62 | static String valueOf(int i)
此方法返回 int 参数的字符串表示形式。 |
| 63 | static String valueOf(long l)
此方法返回 long 参数的字符串表示形式。 |
| 64 | static String valueOf(Object obj)
此方法返回 Object 参数的字符串表示形式。 |