Java中的String类是字符串操作类,提供了多种方法对字符串进行操作,以下对String类的常用方法进行总结:
String类的常用方法列表
图示:
图示2:截图一
String类简介
String类位于Java的lang包下,在使用时不需要通过import引入,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,对象创建后不可修改,由0或多个字符组成,包含在一对双引号之间。
String类创建对象
用String类创建对象通常有两种形式:
-
String str = " 输入字符串";
-
String str1 = new String(“输入字符串”);
方法1通过字面量直接给字符串进行赋值,在栈中创建字符串str时,会先在字符串常量池中检查是否已有该字符串,如果没有则创建并存入字符串常量池中,如果已存在则直接从常量池中获取。
方法2创建的字符串,不管内存有没有为“chengjunyu”字符串分配内存,都会在内存中重新分配一个内存区域存放“chengjunyu”,然后用str1指向它,相当于内存中有两个“chengjunyu”,只是存放的地址不一样。
String类的构造方法
- public String();
String类的无参构造方法,用来创建空字符串的String类;
案例:String str = new String();
- public String(String value);
用已知的字符串value值来创建一个字符串对象;
案例:String str1 = new String(“aaa”);
- public String(char[] value)
用字符数组value创建一个String对象。
案例:char[] value = {“ab”,“cd”,“efg”};
String str1 = new String(value); (等同于new String("abcdefg"));
- public String(char[] value, int startIndex, int numChars)
用字符数组chars的startIndex开始的numChars个字符创建一个String对象。
案例:char[] value = {“ab”,“cd”,“ef”,“g”};
String str2 = new String(value,1,2); (等同于new string("cdef"));
- public String(byte[] values)
用byte的数组value值来创建字符串对象。
案例:byte[] values = new byte[]{“a”,“b”,“c”,“d”,“e”};
String str3 = new String(values); (等同于new String("abcde"))
String类的常用方法
1、获取字符串长度:
str.length();
2、获取字符在字符传中出现的位置:
str.indexOf(String str);
从头开始查找str在字符串中第一次出现的位置;
str.indexOf(String str,int fromIndex);
从下标fromIndex处开始开始查找str在字符串中第一次出现的位置;
str.lastIndexOf(String str);
从尾部开始查找str在字符串中最后一次出现的位置;
str.lastIndexOf(String str,int fromIndex);
从下标fromIndex处开始开始查找str在字符串中最后一次出现的位置;
3、获取字符串中某一个位置的字符:
str.charAt(int index);
查找字符串中下标为index位置出现的字符串
4、截取字符串:
str.substring(fromIndex,endIndex);
从fromIndex处开始截取到endIndex处结束,不包含下标为endIndex的字符;
str.substring(fromIndex);
从fromIndex处开始截取到最后;
5、验证字符串开始或结束的字符:
开始位置字符:str.startWith(String str1),返回Boolean值;
结束位置字符:str.endWith(String str1),返回Boolean值;
6、字符串的比较:
int num = str.compareTo(String str1);
对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
int num = str.compareToIgnore(String str1);
和compareTo()方法类似,忽略大小写。
boolean b = str.equals(Object obj);
比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
boolean b = str.equalsIgnoreCase(Object obj);
和equals方法类似,忽略大小写。
7、字符串连接:
str2 = str.concat(String str1);
用concat连接两个字符城成为一个新的字符串。
8、字符串中字符的大小写转换:
小写转大写:str.toUpperCase();
大写转小写:str.toLowerCase();
9、字符串中字符的替换:
str.replace(oldStr,newStr);
用新的字符代替就得字符。
10、字符串中清除空格:
str.trim();
清除字符串str两侧的空字符串。
11、字符传中分割字符串为数组:
str.split(String str1);
用str1将字符串str分割成数组。
public class method {
public static void method1() {
String str1 = "2020年2月9日" + "abcDEF";
//1.获取字符串的长度
int len = str1.length();
System.out.println("1.获取字符串的长度" + len);
int num1 = str1.indexOf("年");//从头开始查找“年”在字符串中第一次出现的位置
int num2 = str1.indexOf("年", 2);//从下标2开始查找“年”在字符串中第一次出现的位置
int num3 = str1.lastIndexOf("月");//从头开始查找 “月”在字符串中最后一次出现的位置
int num4 = str1.lastIndexOf("2", 4);//从下标4处开始查找 “月”在字符串中最后一次出现的位置
System.out.println("2.获取字符在字符串中出现的位置\n" + num1 + "\n" + num2 + "\n" + num3 + "\n" + num4);
char num5 = str1.charAt(8);
System.out.println("3.获取字符串中某一位置的字符\n" + num5);
String num6 = str1.substring(5);
String num7 = str1.substring(0, 5);
System.out.println("4.截取字符串\n" + num6 + "\n" + num7);
boolean num8 = str1.startsWith("2");
boolean num9 = str1.endsWith("日");
System.out.println("5.验证字符串开始或结束的字符\n" + num8 + "\n" + num9);
}
public static void method2() {
String str1 = "2020年2月9日" + "abcDEF";
String str2 = new String("2018年12月15日" + "ABCdef");
int num10 = str1.compareTo(str2);
int num11 = str1.compareToIgnoreCase(str2);
boolean num12 = str1.equals(str2);
boolean num13 = str1.equalsIgnoreCase(str2);
System.out.println("6.字符串的比较\n" + num10 + "\n" + num11 + "\n" + num12 + "\n" + num13);
String num14 = str2.concat(str1);
System.out.println("7.字符串连接\n" + num14);
String num15 = str1.toUpperCase();
String num16 = str2.toLowerCase();
System.out.println("8.字符串中字符的大小写转换\n" + num15 + "\n" + num16);
String num17 = str1.replace("abcDEF", " 武汉加油 ");
String num18 = str2.replace("ABCdef", " 中国加油 ");
System.out.println(num17 + str1.length());
System.out.println(num18 + str2.length());
String num19 = " 中南民族大学 ";
System.out.println(num19 + num19.length());
String num20 = num19.trim();
System.out.println(num20 + num20.length());
String num21 = "晚风,花间,寺中人";
String[] num22 = num21.split(",");
System.out.println(num21);
for (int i = 0; i < num22.length; i++) {
System.out.print(num22[i]);
}
System.out.println("\t" + num22.length);
}
public static void method3() {
String num19 = " 中南民族大学 ";
System.out.println(num19 + num19.length());
String num20 = num19.trim();
System.out.println(num20 + num20.length());
String num21 = "晚风,花间,寺中人";
String[] num22 = num21.split(",");
System.out.println(num21);
for (int i = 0; i < num22.length; i++) {
System.out.print(num22[i]);
}
System.out.println("\t" + num22.length);
}
}
/*
String常用方法列表练习
*/
public class StringTest {
public static void main(String[] args) {
method.method1();
method.method2();
method.method3();
}
}
1.获取字符串的长度15
2.获取字符在字符串中出现的位置
4
4
6
2
3.获取字符串中某一位置的字符
日
4.截取字符串
2月9日abcDEF
2020年
5.验证字符串开始或结束的字符
true
false
6.字符串的比较
1
1
false
false
7.字符串连接
2018年12月15日ABCdef2020年2月9日abcDEF
8.字符串中字符的大小写转换
2020年2月9日ABCDEF
2018年12月15日abcdef
2020年2月9日 武汉加油 15
2018年12月15日 中国加油 17
中南民族大学 10
中南民族大学6
晚风,花间,寺中人
晚风花间寺中人 3
中南民族大学 10
中南民族大学6
晚风,花间,寺中人
晚风花间寺中人 3
进程已结束,退出代码 0