Java中String类的几个重要方法
最近在算法题中发现String类的方法用到的非常多,如果对String类方法没有一个深度的掌握的话,基础将会非常的不牢固。
由于String类的方法有40几个,我找出了我之前刷算法题经常用到的10几个方法,按照自己理解的增删查改的方式来分类。
一、查询
- 获取字符串长度,length()
- 查询,返回指定索引处的字符,charAt()
- 字符串比较,compareTo()
- 字符串与指定的对象比较,equals()
- 返回指定字符在字符串中第一次出现处的索引,indexOf()
- 返回字符串的子字符串,substring()
1.获取字符串长度,length()
public class Test {
public static void main(String[] args) {
char[] cha = {'c','h','a','r'};
String s = new String(cha);
s.length();
System.out.println(s.length());
}
}
2.查询,返回指定索引处的字符,charAt()
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
String s = "String";
char a = s.charAt(2);
System.out.println(a);
}
}
3.字符串比较,compareTo()
compareTo() 方法用于两种方式的比较:
- 字符串与对象进行比较。int compareTo(Object o)
- 按字典顺序比较两个字符串。int compareTo(String anotherString)
如果参数字符串等于此字符串,则返回值 0;
如果此字符串小于字符串参数,则返回一个小于 0 的值;
如果此字符串大于字符串参数,则返回一个大于 0 的值。
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
String s = "String";
String s1 = "String123";
String s2 = "String123456";
int i = s.compareTo(s);
int i1 = s.compareTo(s2);
int i2 = s2.compareTo(s1);
System.out.println(i);
System.out.println(i1);
System.out.println(i2);
}
}
4.字符串与指定的对象比较,equals()
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
String str = "a";
String str1 = "a";
System.out.println(str.equals(str1));
}
}
5. 返回指定字符在字符串中第一次出现处的索引,indexOf()
indexOf() 方法有以下四种形式:
- public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
- public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
- int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
- int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
String str = "abacsdf";
//查找第一次出現a字符的位置
System.out.println(str.indexOf("a"));
//從2位置開始查找第一次出現a字符的位置
System.out.println(str.indexOf("a",1));
//結合ACILL碼查找
System.out.println(str.indexOf(99));
System.out.println(str.indexOf(99,1));
}
}
6.返回字符串的子字符串,substring()
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
String str = "hellowrold";
//从第几个字符开始的子字符串开始
String str3 = str.substring(2);
System.out.println(str3);
//从第几个字符开始的子字符串;到第几个字符结束
String str2 = str.substring(2,10);
System.out.println(str2);
}
}
二、增加
- 连接两个字符串,concat()
- 用字符数组创建字符串,new String()
1.连接两个字符串,concat()
public class Test {
public static void main(String[] args) {
char[] cha = {'c','h','a','r'};
String s = new String(cha);
String s1 = new String(cha);
//返回两个的连接的新字符串
String s2 = s1.concat(s);
System.out.println(s2);
}
}
2. 用字符数组创建字符串,new String()
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
char[] cha = {'c','h','a','r'};
String s = new String(cha);
System.out.println(s);
}
}
删除
- 删除字符串的头尾空白符, trim()
1.删除字符串的头尾空白符, trim()
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
String str = " hellowrold ";
System.out.println(str);
System.out.println(str.trim());
}
}
修改
- 将数组转为字符串,copyValueOf()
- 将字符从字符串复制到目标字符数组,getChars()
- 替换字符,replace()
- 根据匹配给定的正则表达式来拆分字符串,split()
- 将字符串转换为字符数组, toCharArray()
1.将数组转为字符串,copyValueOf()
copyValueOf() 方法有两种形式:
- public static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的字符串。
- public static String copyValueOf(char[] data, int offset, int count): 返回指定数组中表示该字符序列的 字符串。
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
char[] c = {'a','b','c','d'};
String str = "a";
String str2=str.copyValueOf(c);
String str3=str.copyValueOf(c,0,3);
System.out.println(str2);
System.out.println(str3);
}
}
2. 将字符从字符串复制到目标字符数组,getChars()
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
String str = "abacsdf";
char[] str1 = new char[10];
str.getChars(2,5,str1,0);
System.out.println(str1);
}
}
3.替换字符,replace()
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
String str = "hellowrold";
String str2 = str.replace('o','c');
System.out.println(str2);
}
}
4.根据匹配给定的正则表达式来拆分字符串,split()
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
String str = "hello-wrold-haha";
String[] str1 = str.split("-");
System.out.println(str1[0]);
System.out.println(str1[1]);
System.out.println(str1[2]);
}
}
5.将字符串转换为字符数组, toCharArray()
public class Test {
public static void main(String[] args) {
//用字符Char拼接字符串String
String str = "hellowrold";
char[] c = str.toCharArray();
System.out.println(c);
System.out.println(c[5]);
}
}