前言
字符串是常量;它们的值在创建之后不能更改。字符串缓冲区支持可变的字符串。因为 String 对象是不可变的,所以可以共享,线程安全。
提示:以下是本篇文章正文内容,下面案例可供参考
一、常用构造方法
类型 | 方法及描述方法及描述 |
---|---|
String() | 初始化一个新创建的 String 对象。 |
String(byte[] bytes) | 初始化一个新创建的 String 对象,将字节数组转为字符串。 |
String(byte[] bytes, int offset, int length) | 初始化一个新创建的 String 对象,将字节数组转为字符串。 |
String(char[] value) | 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。 |
String(char[] value, int offset, int count) | 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。 |
String(String original) | 初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列 |
public String()
String(byte[] bytes)
String(byte[] bytes, int offset, int length)
bytes - 要解码为字符的 byte
offset - 要解码的第一个 byte 的索引
length - 要解码的 byte 数
String(char[] value)
**String(char[] value, int offset, int count) **
value - 作为字符源的数组。
offset - 初始偏移量。
count - 长度。
String(String original)
二、String类的判断功能
类型 | 方法及描述方法及描述 |
---|---|
Boolean equals(Object obj) | 将此字符串与指定的 CharSequence 比较是否相等,返回值。 |
boolean equalsIgnoreCase(String s) | 相同位置,忽略大小写,大小写不同认为相同。并返回值 |
boolean contains(CharSequence s) | 当且仅当此字符串包含指定的 char 值序列时,返回 true。 |
boolean startsWith(String s) | 测试此字符串是否以指定的前缀开始。 |
boolean endsWith(String s) | 测试此字符串是否以指定的后缀结束。 |
public boolean isEmpty() | 当且仅当 length() 为 0 时返回 true。 |
Equals() 方法
EqualsIgnoreCase(String str) 方法
Contains() 方法
startsWith() 方法
endsWith() 方法
isEmpty() 方法
三、String类的获取功能
类型 | 方法及描述方法及描述 |
---|---|
public int length() | 返回此字符串的长度。长度等于字符串中 Unicode 代码单元的数量 |
public char charAt(int index) | 返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。 |
public int indexOf(int ch) | 回指定字符在此字符串中第一次出现处的索引。 |
public int indexOf(String str) | 返回指定子字符串在此字符串中第一次出现处的索引。 |
public int indexOf(int ch,int fromIndex) | 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。 |
public int indexOf(String str,int fromIndex) | 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。 |
substring(int beginIndex) | 返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。 |
substring(int beginIndex,int endIndex) | 返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。 |
Length() 方法
charAt() 方法
indexOf(int ch) 方法
indexOf(iString str) 方法
indexOf(int ch,int fromIndex) 方法
indexOf(String str,int fromIndex) 方法
substring(int beginIndex) 方法
substring(int beginIndex,int endIndex) 方法
四、String类的转换功能
类型 | 方法及描述方法及描述 |
---|---|
public byte[] getBytes() | 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 |
public char[] toCharArray() | 将此字符串转换为一个新的字符数组。 |
public static String valueOf(char[] data) | 返回 char 数组参数的字符串表示形式。 |
public static String valueOf(int i) | 返回 int 参数的字符串表示形式。 |
public String toLowerCase() | 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。 |
public String toUpperCase(Locale locale) | 使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。 |
public String concat(String str) | 将指定字符串连接到此字符串的结尾。 |
public String[] split(String regex) | 根据给定正则表达式的匹配拆分此字符串。 |
getBytes() 方法
toCharArray() 方法
valueOf(char[] data) 方法
valueOf(int i) 方法
toLowerCase() 方法
toUpperCase() 方法
concat() 方法
split() 方法
五、String类的其他功能
类型 | 方法及描述方法及描述 |
---|---|
String replace(char oldChar, char newChar) | 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 |
String replace(String target, String replacement) | 使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。该替换从字符串的开头朝末尾执行。 |
String trim() | 返回字符串的副本,忽略前导空白和尾部空白。 |
Int compareTo(String anotherString) | 按字典顺序比较两个字符串。 |
int compareToIgnoreCase(String str) | 按字典顺序比较两个字符串,不考虑大小写。 |
public StringBuffer() 方法
StringBuffer(String str) 方法
toString() 方法