本文是整理常用类库 - String类 的一些常用方法。资料来源自JDK11API。
一、概念知识
- 任何一个字符串都是 String类的对象(实例)。
- 字符串是不变的; 它们的值在创建后无法更改。
字符串内部使用 char 型 数组 ,因为数组确定长度后无法改变,所以字符串无法改变长度(固定长度)
- 因为String对象是不可变的,所以可以共享它们。
- 2个字符串若内容相同,则使用同一块内存地址。
二、常用构造方法
1、String(char[] value, int offset, int length)
生成一个子串
参数:
value - 作为字符源的数组
offset - 起始字符
count - 截取长度
char[] ch = {'a','b','c','d','e'};
String str = new String(ch,1,2);
System.out.println("--------");
System.out.println(str);
结果
三、常用方法
1、public char charAt(int index)
传入一个下标,返回一个char型字符
String text = "abcdefg";
char c = text.charAt(3);
System.out.println(c);
结果
d
2、public int compareTo(String anotherString)
按字典顺序比较两个字符串。如果此String对象按字典顺序排在参数字符串之前,则结果为负整数。 如果此String对象按字典顺序跟随参数字符串,则结果为正整数。 如果字符串相等,结果为零。
String text = "abcdefg";
String text1 = "abcdef";
int res = text.compareTo(text1);
System.out.println(res);
结果
1
3、public int length()
返回此字符串的长度。
4、public char[] toCharArray()
将此字符串转为字符数组,其长度为原字符串的长度,其内容初始化为包含此字符串表示的字符序列。
String text = "abcdefg";
char[] c = text.toCharArray();
System.out.println(c);
for (char ch: c) {
System.out.println(ch);
}
结果
abcdefg
a
b
c
d
e
f
g
5、public boolean equals(Object anObject)
比较串是否相等。具体指:判断两个变量是否是对同一个对象的引用,即堆中的内容是否相同。
6、public boolean equalsIgnoreCase(String anotherString)
忽略大小写比较串是否相等。
String text = "abcdefg";
String text2 = "abcdefG";
System.out.println(text.equals(text2));
System.out.println(text.equalsIgnoreCase(text2));
结果
false
true
7、public boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结尾。
String text2 = "hello.html";
System.out.println(text2.endsWith(".html"));
System.out.println(text2.endsWith("html"));
true
true
同理:
- public boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开头。
8、public String concat(String str)
把参数里的字符串连接到此字符串的末尾。
String text = "abcdefg";
String text2 = "hello.html";
String str = text.concat(text2);
abcdefghello.html
9、public boolean contains(CharSequence s)
当此字符串包含参数里的char值序列时,返回true。
String text = "abcdefg";
String text2 = "cdef";
System.out.println(text.contains(text2));
true
10、public int indexOf(int ch)
返回指定字符ch在该字符串中第一次出现的位置下标。
String text = "abcdaabc";
System.out.println(text.indexOf("a"));
0
11、public int indexOf(int ch, int fromIndex)
从指定的索引处fromIndex开始,返回指定字符ch在该字符串中第一次出现的位置下标。
String text = "abcdaabc";
System.out.println(text.indexOf("a",1));
4
12、public int indexOf(String str)
返回子字符串str在该字符串中第一次出现的位置下标(返回子串的第一个字符的下标)。
String text = "abcdaabc";
System.out.println(text.indexOf("cd"));
2
13、public int indexOf(String str, int fromIndex)
从指定的索引处fromIndex开始,返回子字符串str在该字符串中第一次出现的位置下标(返回子串的第一个字符的下标)。
String text = "abcdaabc";
System.out.println(text.indexOf("abc",1));
5
14、public int lastIndexOf(int ch)
从前往后搜索,返回指定字符ch在该字符串中最后一次出现的下标。
String text = "abcdaabc";
System.out.println(text.lastIndexOf("b"));
6
同理还有
-
public int lastIndexOf(int ch, int fromIndex)
从指定的索引开始向后搜索,返回指定字符ch在该字符串中最后一次出现的下标。 -
public int lastIndexOf(String str,)
-
public int lastIndexOf(String str, int fromIndex)
14、public boolean isEmpty()
length()是 0 时返回 true
String text = "abcdaabc";
System.out.println(text.isEmpty());
String text2 = "";
System.out.println(text2.isEmpty());
String text3 = " ";
System.out.println(text3.isEmpty());
false
true
false
15、public String repeat(int count)
返回一个此字符串重复count后得到的字符串
String text = "abcdaabc";
System.out.println(text.repeat(3));
abcdaabcabcdaabcabcdaabc
16、public String replace(char oldChar, char newChar)
把此字符串里oldChar替换为newChar 。
String text = "abcdaabc";
System.out.println(text.replace('a','A'));
AbcdAAbc
17、public String replace(CharSequence target, CharSequence replacement)
把子串target替换为子串replacement。 替换从字符串的开头到结尾,例如,在字符串“aaa”中将“aa”替换为“b”将导致“ba”而不是“ab”。
String text = "abcdaabc";
System.out.println(text.replace("abc","ABC"));
ABCdaABC
18、public boolean matches(String regex)
判断此字符串是否与给定的正则表达式匹配。
String text = "abcdaabc";
System.out.println(text.matches("\\w+")); // \\w+表示包含至少一个单词字符(字母数字下划线)
true
19、
- public String replaceAll(String regex, String replacement)
使用正则表达式匹配此字符串的每个子字符串,替换为replacement - public String replaceFirst(String regex, String replacement)
使用正则表达式匹配此字符串的第一个子字符串替换为replacement。
String text = "abcdaabc";
System.out.println(text.replaceAll("\\w+","2"));
System.out.println(text.replaceFirst("\\w","2"));
2
2bcdaabc
20、substring 截取字符串
- String substring(int beginIndex) :从beginIndex位置开始截取到末尾,返回一个子串(备注:包含beginIndex)
String a = "hello";
System.out.println(a.substring(1));
结果
ello
- String substring(int beginIndex, int endIndex) :从beginIndex位置开始截取到endIndex,返回一个子串 (备注:包含beginIndex,不包含endIndex)
String a = "hello";
System.out.println(a.substring(1,3));
结果
el