Java Strings 类:掌握字符串创建与操作
字符串在 Java 编程中广泛使用,是字符序列。Java 中字符串作为对象处理。
Java 平台提供 String 类创建和操作字符串。
创建字符串
最直接方式:
String greeting = "Hello world!";
编译器遇到字符串字面量时创建值为 "Hello world!" 的 String 对象。
像其他对象一样,可用 new 关键字和构造器创建 String 对象。String 类有 11 个构造器,允许用不同来源提供字符串初始值,如字符数组。
示例
Live Demopublic 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 类。
字符串长度
获取对象信息的方法称 访问器方法。length() 方法返回字符串对象包含的字符数。
示例
Live Demopublic 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!"
示例:
示例
Live Demopublic 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() 可创建可复用的格式化字符串,而非一次性打印语句。例如不写:
示例
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);
String 方法
String 类支持的方法列表:
| 序号 | 方法 & 描述 |
|---|---|
| 1 | char charAt(int index) - 返回指定索引处字符。 |
| 2 | int compareTo(Object o) - 比较此 String 与另一 Object。 |
| 3 | int compareTo(String anotherString) - 按字典序比较两字符串。 |
| 4 | int compareToIgnoreCase(String str) - 忽略大小写按字典序比较。 |
| 5 | String concat(String str) - 将指定字符串连接到此字符串末尾。 |
| 6 | boolean contentEquals(StringBuffer sb) - 此 String 与指定 StringBuffer 字符序列相同则返回 true。 |
| 7 | static String copyValueOf(char[] data) - 返回数组中字符序列代表的 String。 |
| 8 | static String copyValueOf(char[] data, int offset, int count) - 返回数组指定范围字符序列代表的 String。 |
| 9 | boolean endsWith(String suffix) - 测试此字符串是否以指定后缀结束。 |
| 10 | boolean equals(Object anObject) - 比较此字符串与指定对象。 |
| 11 | boolean equalsIgnoreCase(String anotherString) - 忽略大小写比较此 String 与另一 String。 |
| 12 | byte[] getBytes() - 用平台默认字符集编码此 String 为字节序列,存入新字节数组。 |
| 13 | byte[] getBytes(String charsetName) - 用指定字符集编码此 String 为字节序列,存入新字节数组。 |
| 14 | void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) - 将此字符串字符复制到目标字符数组。 |
| 15 | int hashCode() - 返回此字符串哈希码。 |
| 16 | int indexOf(int ch) - 返回此字符串中首次出现指定字符的索引。 |
| 17 | int indexOf(int ch, int fromIndex) - 从指定索引开始搜索首次出现指定字符的索引。 |
| 18 | int indexOf(String str) - 返回首次出现指定子字符串的索引。 |
| 19 | int indexOf(String str, int fromIndex) - 从指定索引开始搜索首次出现指定子串的索引。 |
| 20 | String intern() - 返回字符串对象的规范表示。 |
| 21 | int lastIndexOf(int ch) - 返回最后一次出现指定字符的索引。 |
| 22 | int lastIndexOf(int ch, int fromIndex) - 从指定索引向后搜索最后一次出现指定字符的索引。 |
| 23 | int lastIndexOf(String str) - 返回指定子串最右出现处索引。 |
| 24 | int lastIndexOf(String str, int fromIndex) - 从指定索引向后搜索指定子串最后出现索引。 |
| 25 | int length() - 返回此字符串长度。 |
| 26 | boolean matches(String regex) - 判断此字符串是否匹配给定正则表达式。 |
| 27 | boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) - 测试两字符串区域是否相等。 |
| 28 | boolean regionMatches(int toffset, String other, int ooffset, int len) - 测试两字符串区域是否相等。 |
| 29 | String replace(char oldChar, char newChar) - 返回将此字符串中所有 oldChar 替换为 newChar 的新字符串。 |
| 30 | String replaceAll(String regex, String replacement) - 将匹配给定正则表达式的每个子串替换为指定替换串。 |
| 31 | String replaceFirst(String regex, String replacement) - 替换首个匹配给定正则表达式的子串。 |
| 32 | String[] split(String regex) - 根据给定正则表达式匹配拆分此字符串。 |
| 33 | String[] split(String regex, int limit) - 根据给定正则表达式匹配拆分此字符串。 |
| 34 | boolean startsWith(String prefix) - 测试此字符串是否以指定前缀开始。 |
| 35 | boolean startsWith(String prefix, int toffset) - 测试此字符串从指定索引开始是否以指定前缀开始。 |
| 36 | CharSequence subSequence(int beginIndex, int endIndex) - 返回此序列的子序列新字符序列。 |
| 37 | String substring(int beginIndex) - 返回此字符串的子字符串。 |
| 38 | String substring(int beginIndex, int endIndex) - 返回此字符串的子字符串。 |
| 39 | char[] toCharArray() - 将此字符串转为新字符数组。 |
| 40 | String toLowerCase() - 用默认区域规则将此 String 所有字符转小写。 |
| 41 | String toLowerCase(Locale locale) - 用给定 Locale 规则将此 String 所有字符转小写。 |
| 42 | String toString() - 返回此对象(本身已是字符串)。 |
| 43 | String toUpperCase() - 用默认区域规则将此 String 所有字符转大写。 |
| 44 | String toUpperCase(Locale locale) - 用给定 Locale 规则将此 String 所有字符转大写。 |
| 45 | String trim() - 返回去除前导尾随空白的字符串副本。 |
| 46 | static String valueOf(primitive data type x) - 返回传入数据类型参数的字符串表示。 |
java
- 精通 Java 字符串:创建、方法与最佳实践
- 深入理解 Java final 关键字:变量、方法与类的不可变性
- Java instanceof 运算符完全指南:用法、继承与接口检查
- 深入理解 Java 静态嵌套类:用法、差异与实战示例
- 精通 Java 匿名内部类:定义、语法与实战示例
- Java 单例模式详解:安全实现与使用指南
- 精通 Java 枚举字符串表示:默认行为、toString 重写与进阶模式
- 精通 Java 反射:运行时检查类、方法与字段
- 掌握 Java ObjectOutputStream:序列化、核心方法与实战示例
- 掌握 Java 泛型:构建可复用、类型安全的代码
- 掌握 Java 文件操作:java.io 包下的创建、读取、写入与删除
- Java Strings 类:掌握字符串创建与操作