String类的方法使用
-从JDK中得出:
-String类代表字符串,字符串字面值"abc"也可以看作一个字符串对象
String str = "abc"; //相当于Person p = new Person(); "abc"相当于new Person();
-字符串是常量,一旦被赋值,就不能被更改 ,指的是"abc"这个对象的内容不能更改
public static void main(String[] args) {
String str = "abc";
str = "def"; //当把"def"这个字符串赋值给str,"abc"就被当作垃圾回收了
System.out.println(str); //String类重写了toString方法,返回的使对象本身,而不是地址值
}
-构造方法:
* public String():空构造
* public String(byte[] bytes):把字节数组转成字符串
* public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
* public String(char[] value):把字符数组转成字符串
* public String(char[] value,int index,int count):把字符数组的一部分转成字符串
* public String(String original):把字符串常量值转成字符串
public static void main(String[] args) {
String str1 = new String();
System.out.println(str1); //输出空串
byte[] arr1 = new byte[]{97,98,99,100,101,102};
String str2 = new String(arr1); //通过解码,将字节数组转换成字符串
System.out.println(str2);
String str3 = new String(arr1, 2, 3); //输出字节数组索引2之后的3个转码后的字符串
System.out.println(str3);
char[] arr2 = new char[]{'a','b','c','d','e','f'};
String str4 = new String(arr2); //将字符数组转换成字符串
System.out.println(str4);
String str5 = new String(arr2,1,3); //输出字符数组索引1之后的3个转码后的字符串
System.out.println(str5);
String str6 = new String("str6");
System.out.println(str6);
}
-面试题:
/* 1.判断定义为String类型的s1和s2是否相等
* String s1 = "abc";
* String s2 = "abc";
* System.out.println(s1 == s2);
* System.out.println(s1.equals(s2));
* 2.下面这句话在内存中创建了几个对象?
* String s1 = new String("abc");
* 3.判断定义为String类型的s1和s2是否相等
* String s1 = new String("abc");
* String s2 = "abc";
* System.out.println(s1 == s2);
* System.out.println(s1.equals(s2));
* 4.判断定义为String类型的s1和s2是否相等
* String s1 = "a" + "b" + "c";
* String s2 = "abc";
* System.out.println(s1 == s2);
* System.out.println(s1.equals(s2));
* 5.判断定义为String类型的s1和s2是否相等
* String s1 = "ab";
* String s2 = "abc";
* String s3 = s1 + "c";
* System.out.println(s3 == s2);
* System.out.println(s3.equals(s2));*/
public static void main(String[] args) {
demo1();
System.out.println("---------------------");
demo2();
System.out.println("---------------------");
demo3();
System.out.println("---------------------");
demo4();
System.out.println("---------------------");
demo5();
}
private static void demo5() {
String s1 = "ab";
String s2 = "abc";
String s3 = s1 + "c"; //string类在使用"+"来连接两个数值时,会在堆内存中创建 StringBuffer对象,再调用
System.out.print(s3 == s2); //toString方法转换成字符串,最后变量指向的地址时tostring的地址
System.out.println(s3.equals(s2));
}
private static void demo4() {
String s1 = "a" + "b" + "c"; //java中有常量保护机制,编译器在编译时s1="abc"
String s2 = "abc";
System.out.print(s1 == s2); //此时指向同一地址
System.out.println(s1.equals(s2));
}
private static void demo3() {
String s1 = new String("abc");
String s2 = "abc";
System.out.print(s1 == s2); //s1中保存的是堆内存的地址,s2中保存的是常量池的地址
System.out.println(s1.equals(s2));
}
private static void demo2() {
String s1 = new String("abc"); //两个,首先在常量池中创建"abc"常量,然后在堆内存中创建new string()对象
System.out.println(s1);
}
private static void demo1() {
String s1 = "abc"; //当给string对象赋值时,首先会在常量池中检测是否已含有,若有,直接调用
String s2 = "abc"; //若无,则创建后调用
System.out.print(s1 == s2); //"=="号比较的时两个字符串的地址值
System.out.println(s1.equals(s2)); //"equals()"比较的是对象的值
}
-demo5的内存图解
-String类的判断功能:
* boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
* boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
* boolean contains(String str):判断大字符串中是否包含小字符串
* boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
* boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
* boolean isEmpty():判断字符串是否为空
-String类的获取功能:
* int length():获取字符串的长度
* char charAt(int index):获取指定索引位置的字符
* int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引
* int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引
* int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
* int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引
* lastIndexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后最后一次出现处的索引
* String substring(int start):从指定位置开始截取字符串,默认到末尾
* String substring(int start,int end):从指定位置开始到指定位置结束截取字符串
-字符串的遍历:
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
for(int i=0;i<str.length();i++){
char c = str.charAt(i); //获取指定位置的字符
System.out.print(c+" ");
}
}
-范例:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int c1= 0;
int c2 = 0;
int c3 = 0;
System.out.println("请输入字符串");
String str = sc.nextLine();
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
if(c>='A'&&c<='Z'){
c1++;
}else if(c>='a'&&c<='z'){
c2++;
}else if(c>='0'&&c<='9'){
c3++;
}
}
System.out.println("大写的字符有"+c1+"个");
System.out.println("小写的字符有"+c2+"个");
System.out.println("数字字符有"+c3+"个");
}
-String类的转换功能:
* byte[] getBytes():把字符串转换为字节数组
* char[] toCharArray():把字符串转换为字符数组
* static String valueOf(char[] chs):把字符数组转成字符串
* static String valueOf(int i):把int类型的数据转成字符串
* 注意:String类的valueOf方法可以把任意类型的数据转成字符串
* String toLowerCase():把字符串转成小写(了解)
* String toUpperCase():把字符串转成大写
* String concat(String str):把字符串拼接
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String str = sc.nextLine();
byte[] arr1 = str.getBytes(); //通过gbk码表将字符串转换为子节数组
for (int i = 0; i < arr1.length; i++) { //gbk码表一个中文字符代表两个子节
System.out.print(arr1[i]+" "); //gbk码表特点,中文的第一个字节一定是负数
}
System.out.println();
char[] arr2 = str.toCharArray();
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i]+" ");
}
System.out.println();
String str1 = String.valueOf(arr1);
String str2 = String.valueOf(arr2);
System.out.println(str1);
System.out.println(str2);
}
-范例:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入英文字符串:");
String str = sc.nextLine();
String str1 = str.substring(0, 1).toUpperCase().concat(str.substring(1,str.length()).toLowerCase());
System.out.println("转换后的字符串为:");
System.out.println(str1);
}
-注:字节转字符计算机查询的是gbk码表,而在字符转码时查询的是unicode码表。
gbk和unicode都包含ASCII码表
-String类的其他功能:
-1.替换字符或字符串
* String replace(char old,char new)
* String replace(String old,String new)
-2.去除字符串两边空格(只能去除两边的空格,不包括中间的)
* String trim()
-3.String的按字典顺序比较两个字符串
* int compareTo(String str)(暂时不用掌握)
* int compareToIgnoreCase(String str)(了解)
-范例:统计大串中小串出现的次数
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入大串:");
String s1 = sc.nextLine();
System.out.println("请输入想要查询的小串:");
String s2 = sc.nextLine();
int count = 0;
int index = 0;
if(!s1.contains(s2)){
System.out.println("no exists!!");
}else{
while((index = s1.indexOf(s2))!=-1){
count++;
s1 = s1.substring(index+s2.length());
}
System.out.println(count);
}
-44.StringBuffer类
-概述:线程安全的可变字符序列,类似于String的字符串缓冲池,但不能修改(不能像String那样通过"+"来改变字符串,可以通过方法来改变)
-Stringbuffer和String的区别:
-String:是一个不可变的字符序列
-StringBuffer:是一个可变的字符序列
-构造方法:
* StringBuffer(); //构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符
*StringBuffer(CharSequence seq
); //构造一个字符缓冲区,包含与指定的CharSequence相同的字符
*StringBuffer(int capacity); //构造一个不带字符,但指定容量的字符缓冲区
*StringBuffer(String str); //构造一个字符缓冲区,并且内容初始化为指定的字符串
-方法:
*public int capacity(); //返回当前容量(理论值)
*public int length(); //返回长度(实际值)
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.print(sb.length()+" "); //容器的字符个数,实际值
System.out.println(sb.capacity()); //容器的理论容量,理论值
StringBuffer sb1 = new StringBuffer(10);
System.out.print(sb1.length()+" ");
System.out.println(sb1.capacity());
StringBuffer sb2 = new StringBuffer("hello");
System.out.print(sb2.length()+" "); //实际字符的个数
System.out.println(sb2.capacity()); //字符串的length+理论容量
}
-StringBuffer的添加功能:
*public StringBuffer append(String str);
-可以把任意类型数据添加到字符串缓冲区中,并返回字符串缓冲区本身
*public StringBuffer insert(int offset,String str);
-在指定位置把任意类型的数据插入到字符串缓冲区中,并返回字符串缓冲区本身,如果没有指定位置索引,就会报索引越界异常
-注:StringBuffer是字符串缓冲区,当new的时候是在堆内存中创建一个对象,底层是一个长度为16的字符数组,当调用添加的方法,不会再创建对象,而是不断向缓冲区添加字符
-StringBuffer的删除功能:
*public StringBuffer delectCharAt(int index);
-删除指定位置的字符,并返回本身
*public StringBuffer delete(int start,int end);
-删除从指定位置开始到指定位置结束的字符,并返回字符,删除时是包含头,不包含尾 => [start,end);
*清空缓冲区:对象.delete(0,对象.length());
-String Buffer的替换功能:
*public StringBuffer replace(int start,int end,String str);
-从start开始到end用str替换,不包含end
-StringBuffer的反转功能:
*public StringBuffer reverse();
-字符串反转
-StringBuffer的截取功能和注意事项:
*public String substring(int start);
-从start截取到末尾
*public String substring(int start,int end);
-从start截取到end
-注:此时的返回值类型是String类型,而不是StringBuffer
-StringBuffer和String的转换:
-1.String--->StringBuffer
*通过构造方法
*通过append()方法
-2.StringBuffer--->String
*通过构造方法
*通过toString()方法
*通过substring()方法
public static void main(String[] args) {
demo1();
demo2();
}
private static void demo2() {
StringBuffer sb = new StringBuffer("hello");
String str = new String(sb); //通过构造方法将StringBuffer对象转换为字符串
System.out.println(str);
String str1 = sb.toString(); //通过toString方法
System.out.println(str1);
String str2 = sb.substring(0,sb.length()); //通过substring方法
System.out.println(str2);
}
private static void demo1() {
StringBuffer sb = new StringBuffer("hello"); //通过构造方法将String转换为StringBuffer对象
System.out.println(sb);
StringBuffer sb1 = new StringBuffer(); //通过append方法将String转换为StringBuffer对象
sb1.append("hello");
System.out.println(sb1);
}
-45.StringBuider类
-概述:是一个可变的字符序列(方法和构造与StringBuffer一致,但是线程不安全的)
-StringBuider和String Buffer的区别:
*String Buffer是JDK1.0版本的,是线程安全的,效率低(相当于给厕所门上了锁,只有使用完别人才能访问,使用时是安全的)同步的
*StringBuider是JDK1.5版本的,是线程不安全的,效率高(门没有上锁,别人随时可以访问,不安全)不同步的
46.String和StringBuffer当作参数传递
public static void main(String[] args) {
String s = new String("hello");
change(s);
System.out.println(s);
System.out.println("-----------------");
StringBuffer sb = new StringBuffer();
sb.append("hello");
change(sb);
System.out.println(sb);
}
public static void change(StringBuffer sb) { //StringBuffer对象作为参数,可实际改变
sb.append("world");
}
public static void change(String s) { //String作为参数传递,当方法弹栈时会消失
s+="world";
}
}