字符串定义及常用方法
定义: 字符串是由多个字符组成的一组数据,java中写在双引号("")中.
String的特性:
-
字符串是常量,一旦创建不能被改变,这是因为字符串的值是被存放在方法区的常量池中,但是引用可以改变。
-
字符串的字面值"student",可以看成是一个字符串对象。
内存图如下:
String常用方法:
获取方法:
- length();获取字符串的长度;
- charAt(int index);获取指定索引位置的字符;
- indexOf(char c);返回指定字符在此字符串中第一次出现处的索引,如果字符串中没有该字符,返回-1;
- indexOf(char c, int index);返回指定字符在此字符串中从指定位置后第一次出现处的索引;
- substring(int index);从指定位置开始截取字符串,默认到末尾;
- substring(int startindex,int endindex);从指定位置开始到指定位置结束截取字符串;
//定义字符串
String str = "asDFGHjk";
//获取字符串的长度
int length = str.length();
//获取指定索引位置的字符
char c1 = str.charAt(0);
//返回指定字符在此字符串中第一次出现处的索引,如果字符串中没有该字符,返回-1;
int c2 = str.indexOf('n');
//返回指定字符串在此字符串中第一次出现的索引,如果字符串中没有该字符,返回-1;
int c3 = str.indexOf("FG");
//返回指定字符在此字符串中从指定位置后第一次出现处的索引。
int c4 = str.indexOf('F', 2);
//返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
int c5 = str.indexOf("FG", 2);
//从指定位置开始截取字符串,默认到末尾
String c6 = str.substring(2);
//从指定位置开始到指定位置结束截取字符串
String c7 = str.substring(2, 4);
System.out.println(length);
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
System.out.println(c5);
System.out.println(c6);
System.out.println(c7);
运行结果:
8
a
-1
3
3
3
DFGHjk
DF
比较方法:
- equals(String str): 比较字符串的内容是否相同,区分大小写;
- equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写;
- contains(String str): 判断字符串中是否包含传递进来的字符串;
- startsWith(String str): 判断字符串是否以传递进来的字符串开头;
- endsWith(String str): 判断字符串是否以传递进来的字符串结尾;
- isEmpty(): 判断字符串的内容是否为空串"";
String str1 = "abcdef";
String str2 = "ABcdef";
//比较字符串的内容是否相同,区分大小写
boolean b = str1.equals(str2);
//比较字符串的内容是否相同,忽略大小写
boolean b1 = str1.equalsIgnoreCase(str2);
//判断字符串中是否包含传递进来的字符串 contains:包含
boolean b2 = str1.contains("cde");
//判断字符串是否以传递进来的字符串开头
boolean b3 = str1.startsWith("ab");
//判断字符串是否以传递进来的字符串结尾
boolean b4 = str2.endsWith("ef");
//判断字符串的内容是否为空 isEmpty:空的
boolean b5 = str1.isEmpty();
System.out.println(b);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
System.out.println(b5);
运行结果:
false
true
true
true
true
false
转换方法:
- getBytes(): 把字符串转换为字节数组;
- toCharArray(): 把字符串转换为字符数组;
- new String(char[] chs): 把字符数组转成字符串;
- Integer.toString(String str): 把int类型的数据转成字符串;
- toUpperCase(): 把字符串转成大写;
- toLowerCase(): 把字符串转成小写;
- concat(String str): 把字符串拼接;
String str1 = "abcdef";
String str2 = "ABcdef";
int i = 123456;
//把字符串转换成字节数组
byte[] bytes = str1.getBytes();
for (Byte b : bytes) {
System.out.print(b + " ");
}
System.out.println();
//将字符串转换成字符数组
char[] chars = str1.toCharArray();
for (char c : chars) {
System.out.print(c + " ");
}
System.out.println();
//将字符数组装换成字符串
String s1 = new String(chars);
System.out.println(s1);
//将int类型的数据转换成字符串
String s2 = Integer.toString(i);
System.out.println(s2);
//将字符串转换成大写
String s3 = str1.toUpperCase();
System.out.println(s3);
//将字符串转换成大写
String s4 = str2.toLowerCase();
System.out.println(s4);
//字符串拼接
String s5 = str1.concat(s2);
System.out.println(s5);
运行结果:
97 98 99 100 101 102
a b c d e f
abcdef
123456
ABCDEF
abcdef
abcdef123456
其他常用功能:
-
trim() 去除两端空格;
-
replace(char old,char new) 将指定字符进行互换;
-
replace(String old,String new) 将指定字符串进行互换;
下面两个不常用:
-
compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果,如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果,如果两个字符串一摸一样 返回的就是0;
-
compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较;
String str1 = " abcdef ";
//将指定字符进行互换
String s1 = str1.replace('a', 'b');
System.out.println(s1);
//将指定字符串进行互换
String s2 = str1.replace("ab", "ff");
System.out.println(s2);
//去除两端空格
String s3 = str1.trim();
System.out.println(s3);
//通过字典顺序去比较 返回的值是 ASCII 码的差值 调用者减去传入者
int i1 = "abc".compareTo("Abc");
System.out.println(i1);
//如果前面几个字母一样会根据两个字符串长度进行减法运算返回该减法的结果 (通过长度去比)
int i2 = "abc".compareTo("ab");
System.out.println(i2);
//忽略大小写的比较 返回ASCII码的差值
int i3 = "abg".compareToIgnoreCase("ABC");
System.out.println(i3);
运行结果:
bbcdef
ffcdef
abcdef
32
1
4