String 类的概述和常用方法
String类的概述
String的概述:
String类表示的字符串。java程序中的所有字符串,如 "abc",实现这个类的实例。
String类的特点:
字符串是常量,它们的值不能被创建后改变。支持可变字符串字符串缓冲区。因为字符串对象是不可改变的,所以它们可以被共享。
例如:
String str = "abc";
相当于:
char ch[] = {'a', 'b', 'c'};
String str = new String(ch);
字符串定义:
字符串是由多个字符组成的一串数据(字符序列)
字符串可以看成是字符数组
String类的length方法:
String str = "abc";
int i = str.length();
System.out.println(i);
String类的构造方法
常见的构造方法:
public String():空构造
public String(byte[] bytes):把字节数组转成字符串
public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
public String(char[] value):把字符数组转成字符串
public String(char[] value,int index,int count):把字符数组的一部分转成字符串
public String(String original):把字符串常量值转成字符串
案例:
public class Boke {
public static void main(String[] args) {
//空构造
String s1 =new String();
byte[] b1 = new byte[]{97, 98, 99, 100, -28, -67, -96};
//把字节数组转换成字符串
String s2 = new String(b1);
//把字节数组的一部分转换成字符串
//offse:起始索引; length:转换长度;
String s3 = new String(b1,4,3);
char[] b2 = new char[]{'a','b','c','d'};
//把字符数组转换成字符串
String s4 = new String(b2);
//把字符数组的一部分转换成字符串
//offset:起始索引
String s5 = new String(b2,1,2);
//把字符串常量转换为字符串
String s6 = new String("锄禾日当午");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
System.out.println(s6);
}
}
结果:
//s1 是空字符串,打印的就是空的
abcd你 //s2 打印字节数组,对照ASCII码表打印每个字节对应的内容,汉字使用的是UTF-8码表
你 //s3 把字节数组 b1 从索引 4 开始向后 3 个字节
abcd //s4 把字符数组 b2 转换成字符串;
bc //S5 把字符数组 b2 从索引 1 开始向后 2 个字符
锄禾日当午 //s6 把字符窜常量转换为字符串;
定义String类的内存图解释
public class MyTest5 {
public static void main(String[] args) {
/* A:
面试题1
String s = new String("hello") 和String s ="hello";
的区别*/
String s = new String("hello");
String s2 = "hello";
String s3 = "abc";
}
}
上面代码的内存图解
String 类比较时"=="和"equals()"以及的区别
在String类中,equals方法被重新,不再比较地址值,而是比较两个字符串字面内容是否相同;
而"=="比较的是地址值。
如上面的内存图解图所示:
s 指向的地址是0x002;字面值是"hello";
s2 指向的地址是0x001;字面值是"hello";
s3 指向的地址是0x003;字面值是"hello";
所以:
s.equals(s2); //true,比较字面值;都是hello;
s == s2 //false,比较地址值,不相等;
但是:
intern()方法是取这个字符串在常量池中的地址;
例:
//获取字符串 s 字面值在字符串常量池里的地址,并赋给引用(s4);
String s4 = s.intern();
//此时 s4 也就指向字符串常量池的 字符串常量("hello");
s2 == s4 //true,因为 s4 和 s2 都指向的是字符串常量池里的"hello"的地址;
String类中的判断功能
/*
public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写
public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写
public boolean contains(String str): 判断字符串中是否包含传递进来的字符串
public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾
public boolean isEmpty(): 判断字符串的内容是否为空串""。
public boolean startsWith(String str): 判断字符串是否以指定字符串开头
public boolean endsWith(String str): 判断字符串是否以指定字符串结尾
*/
public class Boke2 {
public static void main(String[] args) {
String s1="abcdefg";
String s2="ABCDEFG";
//public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写
System.out.println(s1.equals(s2));
//public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2));
//public boolean contains(String str): 判断字符串中是否包含传递进来的字符串
System.out.println(s1.contains("cdef"));
//public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
System.out.println(s1.startsWith("a"));
//public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾
System.out.println(s1.endsWith("g"));
//public boolean isEmpty(): 判断字符串的内容是否为空串""。
System.out.println(s1.isEmpty());
}
}
结果:
false
true
true
true
true
false
Process finished with exit code 0
String类获取的功能
/*
public int length(): 获取字符串的长度。
public char charAt(int index): 获取指定索引位置的字符
public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
public int lastIndexOf(int ch): 与indexOf()的方法类似,只不过lastIndexOf()是从末尾开始
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。
*/
public class Boke3 {
public static void main(String[] args) {
String s = "abcdefgh";
//public int length(): 获取字符串的长度。
System.out.println("字符串的长度:"+s.length());
//public char charAt(int index): 获取指定索引位置的字符
System.out.println("指定索引(3)位置的字符:"+s.charAt(3));
//public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
System.out.println("指定字符(b)在此字符串中第一次出现处的索引:"+s.indexOf("b"));
//public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引(指定字符串中第一个字符的索引)。
System.out.println("指定字符串(cd)在此字符串中第一次出现处的索引:"+s.indexOf("cd"));
//public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
System.out.println("指定字符(f)在此字符串中从指定位置(2:也就是索引是2)后第一次出现处的索引:"+s.indexOf("f",2));
//public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引(指定字符串中第一个字符的索引)。
System.out.println("指定字符串(ef)在此字符串中从指定位置后(2:也就是索引是2)第一次出现处的索引:"+s.indexOf("ef",2));
//可以顺带提一下lastIndexOf系列,lastIndexOf是从字符串的末尾向开头检索,与IndexOf(从开头向末尾检索相反)。
System.out.println("从字符串的末尾向开头检索,返回该字符(c)本来的索引:"+s.lastIndexOf("c"));
//public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
System.out.println("从指定位置(索引2)开始截取字符串,直至末尾:"+s.substring(2));
//public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。(截头不截尾)
System.out.println("从指定位置(索引2)开始到指定位置(索引6)结束截取字符串:"+s.substring(3,6));
}
}
结果:
字符串的长度:8
指定索引(3)位置的字符:d
指定字符(b)在此字符串中第一次出现处的索引:1
指定字符串(cd)在此字符串中第一次出现处的索引:2
指定字符(f)在此字符串中从指定位置(2:也就是索引是2)后第一次出现处的索引:5
指定字符串(ef)在此字符串中从指定位置后(2:也就是索引是2)第一次出现处的索引:4
从字符串的末尾向开头检索,返回该字符(c)本来的索引:2
从指定位置(索引2)开始截取字符串,直至末尾:cdefgh
从指定位置(索引2)开始到指定位置(索引6)结束截取字符串:def
Process finished with exit code 0
String类的转换功能
/*
public byte[] getBytes(): 把字符串转换为字节数组。
public char[] toCharArray(): 把字符串转换为字符数组。
public static String valueOf(char[] chs): 把字符数组转成字符串。
public static String valueOf(int i): 把int类型的数据转成字符串。
//注意:String类的valueOf方法可以把任意类型的数据转成字符串。
public String toLowerCase(): 把字符串转成小写。
public String toUpperCase(): 把字符串转成大写。
public String concat(String str): 把字符串拼接。
*/
import static java.lang.String.valueOf;
public class Boke4 {
public static void main(String[] args) {
String s="abcdefg";
//public byte[] getBytes(): 把字符串转换为字节数组。
System.out.println("字符串转换为字节数组:"+s.getBytes());
//public char[] toCharArray(): 把字符串转换为字符数组。
System.out.println("把字符串转换为字符数组:"+s.toCharArray());
//public static String valueOf(char[] chs): 把字符数组转成字符串。
char[] ch = new char[]{'a','b','c','d','e','f','g'};
System.out.println("把字符数组转成字符串:"+valueOf(ch));
//public static String valueOf(int i): 把int类型的数据转成字符串。
int i=9527;
System.out.println("把int类型的数据转成字符串:"+valueOf(i));
//注意:String类的valueOf方法可以把任意类型的数据转成字符串。
//public String toUpperCase(): 把字符串转成大写。
String s1 = s.toUpperCase();
System.out.println("把字符串转成大写:"+s1);
//public String toLowerCase(): 把字符串转成小写。
String s2 = s1.toLowerCase();
System.out.println("把字符串转成小写:"+s2);
//public String concat(String str): 把字符串拼接。
System.out.println("把字符串拼接:"+s.concat(s1).concat(s2));
}
}
结果:
字符串转换为字节数组:[B@1540e19d //此处打印了数组的地址值,需要遍历的话,可以自行遍历
把字符串转换为字符数组:[C@677327b6 //此处打印了数组的地址值,需要遍历的话,可以自行遍历
把字符数组转成字符串:abcdefg
把int类型的数据转成字符串:9527
把字符串转成大写:ABCDEFG
把字符串转成小写:abcdefg
把字符串拼接:abcdefgABCDEFGabcdefg
Process finished with exit code 0
String类的其他功能
/*
String的替换功能及案例演示
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换
String的去除字符串两空格及案例演示
public String trim() 去除两端空格
String的按字典顺序比较两个字符串及案例演示
public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果。如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果;如果连个字符串一摸一样 返回的就是0
public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较
*/
public class Boke5 {
public static void main(String[] args) {
String s=" abcdefg ";
//public String replace(char old,char new) 将指定字符进行互换
System.out.println("将指定字符进行互换:"+s.replace('c','m'));
//public String replace(String old,String new) 将指定字符串进行互换
System.out.println("将指定字符串进行互换:"+s.replace("cde","ijk"));
//public String trim() 去除两端空格
System.out.println("去除两端空格:"+s.trim());
//String类按照字典顺序比较两个字符串;public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
/* 如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果;如果两个字符串一摸一样 返回的就是0*/
String s1="a";
String s2="b";
System.out.println("按照字典顺序比较两个字符串:"+s1.compareTo(s2));
//public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较
String s3="B";
System.out.println("按照字典顺序比较两个字符串(忽略大小写):"+s1.compareToIgnoreCase(s3));
}
}
结果:
将指定字符进行互换: abmdefg
将指定字符串进行互换: abijkfg
去除两端空格:abcdefg
按照字典顺序比较两个字符串:-1
按照字典顺序比较两个字符串(忽略大小写):-1
Process finished with exit code 0