手册
目录
收藏694
分享
阅读989
更新时间2025-07-22
前言:
在本教程中,您将学习如何使用Java.lang.String类,简介java.lang.String类表示字符串。Java程序中的所有字符串字面量,比如abc,都是作为这个类的实例来实现的。
java.lang.String 类表示字符串。 Java程序中的所有字符串字面量,比如"abc",都是作为这个类的实例来实现的。字符串是常量,创建后它们的值不能改变
以下是 java.lang.String 类的声明 −
public final class String
extends Object
implements Serializable, Comparable, CharSequence
点击 "运行实例" 按钮查看在线实例
以下是 java.lang.String 类的字段 −
static Comparator
| 序号 | 构造函数 & 描述 |
|---|---|
| 1 |
String() 这会初始化一个新创建的 String 对象,以便它表示一个空字符序列。 |
| 2 |
String(byte[] bytes) 这通过使用平台的默认字符集解码指定的字节数组来构造一个新的字符串。 |
| 3 |
String(byte[] bytes, Charset charset) 这通过使用指定的字符集解码指定的字节数组来构造一个新的字符串。 |
| 4 |
String(byte[] bytes, int offset, int length) 这通过使用平台的默认字符集解码指定的字节子数组来构造一个新的字符串 |
| 5 |
String(byte[] bytes, int offset, int length, Charset charset) 这通过使用指定的字符集解码指定的字节子数组来构造一个新的字符串。 |
| 6 |
String(byte[] bytes, int offset, int length, String charsetName) 这通过使用指定的字符集解码指定的字节子数组来构造一个新的字符串。 |
| 7 |
String(byte[] bytes, String charsetName) 这通过使用指定的字符集解码指定的字节数组来构造一个新的字符串。 |
| 8 |
String(char[] value) 这会分配一个新的字符串,以便它表示当前包含在字符数组参数中的字符序列。 |
| 9 |
String(char[] value, int offset, int count) 这会分配一个新字符串,其中包含来自字符数组参数的子数组的字符。 |
| 10 |
String(int[] codePoints, int offset, int count) 这会分配一个新字符串,其中包含来自 Unicode 代码点数组参数的子数组的字符。 |
| 11 |
String(String original) 这会初始化一个新创建的 String 对象,以便它表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本。 |
| 12 |
String(StringBuffer buffer) 这将分配一个新字符串,该字符串包含当前包含在字符串缓冲区参数中的字符序列。 |
| 13 |
String(StringBuilder builder) 这将分配一个新字符串,该字符串包含字符串构建器参数中当前包含的字符序列。 |
| 序号 | 方法 & 描述 |
|---|---|
| 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)
此方法返回此字符串的指定文本范围内的 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)
该方法返回一个字符串,表示指定数组中的字符序列。 |
| 12 | static String copyValueOf(char[] data, int offset, int count)
该方法返回一个字符串,表示指定数组中的字符序列。 |
| 13 | boolean endsWith(String suffix)
此方法测试此字符串是否以指定的后缀结尾。 |
| 14 | boolean equals(Object anObject)
此方法将此字符串与指定对象进行比较。 |
| 15 | boolean equalsIgnoreCase(String anotherString)
此方法将此字符串与另一个字符串进行比较,忽略大小写考虑。 |
| 16 | static String format(Locale l, String format, Object... args)
此方法使用指定的语言环境、格式字符串和参数返回格式化字符串。 |
| 17 | static String format(String format, Object... args)
此方法使用指定的格式字符串和参数返回一个格式化字符串。 |
| 18 | byte[] getBytes()
此方法使用平台的默认字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中。 |
| 19 | byte[] getBytes(Charset charset)
此方法使用给定的字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中。 |
| 20 | byte[] getBytes(String charsetName)
此方法使用命名字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中。 |
| 21 | void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
此方法将此字符串中的字符复制到目标字符数组中。 |
| 22 | int hashCode()
此方法返回此字符串的哈希码。 |
| 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 tooffset, String other, int ooffset, int len)
此方法测试两个字符串区域是否相等且忽略大小写。 |
| 37 | boolean regionMatches(int toffset, String other, int ooffset, int len)
此方法测试两个字符串区域是否相等。 |
| 38 | String replace(char oldChar, char newChar)
此方法返回一个新字符串,该字符串是用 newChar 替换此字符串中所有出现的 oldChar。 |
| 39 | String replace(CharSequence target, CharSequence replacement)
此方法用指定的文字替换序列替换此字符串中与文字目标序列匹配的每个子字符串。 |
| 40 | String replaceAll(String regex, String replacement)
此方法用给定的替换替换此字符串中与给定正则表达式匹配的每个子字符串。 |
| 41 | String replaceFirst(String regex, String 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()
此方法使用默认语言环境的规则将此字符串中的所有字符转换为小写。 |
| 51 | String toLowerCase(Locale locale)
此方法使用给定 Locale 的规则将此 String 中的所有字符转换为小写。 |
| 52 | String toString()
此方法返回字符串本身。 |
| 53 | String toUpperCase()
此方法使用默认语言环境的规则将此字符串中的所有字符转换为大写。 |
| 54 | String toUpperCase(Locale locale)
此方法使用给定 Locale 的规则将此 String 中的所有字符转换为大写。 |
| 55 | String trim()
此方法返回字符串的副本,省略了前导和尾随空格。 |
| 56 | static String valueOf(boolean b)
此方法返回布尔参数的字符串表示形式。 |
| 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)
此方法返回双精度参数的字符串表示形式。 |
| 61 | static String valueOf(float f)
此方法返回浮点参数的字符串表示形式。 |
| 62 | static String valueOf(int i)
此方法返回 int 参数的字符串表示形式。 |
| 63 | static String valueOf(long l)
此方法返回长参数的字符串表示形式。 |
| 64 | static String valueOf(Object obj)
此方法返回 Object 参数的字符串表示形式。 |
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
71万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习