String类讲解 char[] chs = {‘a’,’b’,’c’};
(1) 是由多个字符组成的一串数据。(字符序列)
其实字符串可以看成是一个字符数组。
(2)构造方法:
public String():无参构造方法
public String(byte[] bytes):把字节数组转换为字符串
public String(char[] value):把字符数组转换为字符串
public String(char[] value,int offset,int count):把字符数组的一部分转换为字符串
public String(String original):把一个字符串转换为字符串
(需要利用到的一个成员方法)成员方法:
public int length():返回此字符串的长度
package day4.edu_10;
public class StringDemo1 {
public static void main(String[] args) {
// //public String():无参构造方法
// String s = new String();
// s = "hello";
// System.out.println(s);
System.out.println("-------------------------");
// //public String(byte[] bytes):把字节数组转换为字符串
// String s = "hello";
// byte[] bys = s.getBytes();
// String s2 = new String(bys);
// System.out.println(s2);
System.out.println("--------------------------");
// //public String(char[] value):把字符数组转换为字符串
// char[] chs = {'a','b','c'};
// String s = new String(chs);
// System.out.println(s);
System.out.println("---------------------------");
// //public String(char[] value,int offset,int count):把字符数组的一部分转换为字符串
// char[] chs = {'a','b','c'};
// String s = new String(chs,0,1);//offect:开始的索引 count:从开始索引开始向后获取多少个字符
// System.out.println(s);
System.out.println("-------------------------------");
// //public String(String original):把一个字符串转换为字符串
String s = new String("hello");
System.out.println(s);
// (需要利用到的一个成员方法)成员方法:
// public int length():返回此字符串的长度
System.out.println(s.length());
}
}
(3)String的特点及面试题
A:
String类的数据特点:
字符串是常量;它们的值在创建之后不能更改
面试题:根据以上结论请问输出的s的值是多少
String s = “hello”;
s += “world”;
System.out.println(s);
package day4.edu_10;
public class StringDemo2 {
public static void main(String[] args) {
String s = "hello";
s+="world";
System.out.println(s);
}
}
结论:
字符串的内容不能发生改变,但是字符串引用的指向是可以改变的。
B:String s = new String(“hello”)和String s = “hello”的区别(画图讲解)
前者创建了1个或者2个对象
后缀创建了0个或者1个对象
C:面试题(看程序写结果)
new和直接赋值的区别:
package day4.edu_10;
public class StringDemo3 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3==s4);//false
System.out.println(s3.equals(s4));//true
String s5 = "hello";
String s6 = "hello";
System.out.println(s5==s6);//true
System.out.println(s5.equals(s6));//true
}
}
看程序写结果
package day4.edu_10;
public class StringDemo4 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 ="helloworld";
System.out.println(s3==s1+32);//false
System.out.println(s3.equals(s1 + s2));//true
System.out.println(s3=="helloworld");//true
System.out.println(s3.equals("hello"+"world"));//true
}
}
结论:
变量相加,先开空间,在加。
常量相加,先加,找是否有这样的数据空间,如果没有才开空间。
(4)String类的成员方法
A:判断功能
boolean equals(Object obj):比较两个字符串的内容是否相同,严格区分大小写。(用户名,密码)
boolean equalsIgnoreCase(String str):比较两个字符串的内容是否相同,忽略大小写。(验证码)
boolean contains(String str):判断字符串中是否包含一个子串。
boolean startsWith(String str):判断是否以指定的字符串开头
boolean endsWith(String str):判断是否以指定的字符串结尾
boolean isEmpty():判断字符串的内容是否为空
问题:内容为空和对象为空是一个意思吗?
答:不是
package day4.edu_10;
public class StringDemo5 {
public static void main(String[] args) {
// boolean equals(Object obj):比较两个字符串的内容是否相同,严格区分大小写。(用户名,密码)
String s1 = "hello";
String s2 = "Hello";
System.out.println(s1.equals(s2));
// boolean equalsIgnoreCase(String str):比较两个字符串的内容是否相同,忽略大小写。(验证码)
System.out.println(s1.equalsIgnoreCase(s2));
// boolean contains(String str):判断字符串中是否包含一个子串。
System.out.println(s1.contains("hel"));
// boolean startsWith(String str):判断是否以指定的字符串开头
System.out.println(s1.startsWith("hel"));
// boolean endsWith(String str):判断是否以指定的字符串结尾
System.out.println(s1.endsWith("lo"));
// boolean isEmpty():判断字符串的内容是否为空
System.out.println(s1.isEmpty());
System.out.println("---------------------");
String s3 = null;
String s4 = "";
System.out.println(s4);
// System.out.println(s3.isEmpty());
System.out.println(s4.isEmpty());
/**
* java.lang.NullPointerException:空指针异常
*
* 对象为空:null,说明这个引用没有指向
* 字符串为空:就是一个空字符串,字符串中什么也没有
*/
}
}
B:获取功能
String类的获取功能:
int length():返回字符串的长度。其实就是字符的个数。
char charAt(int index):返回字符串中指定索引处的字符。
int indexOf(int ch):返回指定的字符在字符串中第一次出现的索引。
明明说的是字符,为什么是int呢?
原因是int类型还可以接收char类型。
97,'a'是一样的效果。
但是如果参数是char类型,你就不能写97了。
int indexOf(String str):返回指定的字符串在字符串中第一次出现的索引。
String substring(int start):截取从start开始到末尾的字符串。
String substring(int start,int end):截取从start开始到end结束的字符串。
package day4.edu_10;
public class StringDemo6 {
public static void main(String[] args) {
// int length():返回字符串的长度。其实就是字符的个数。
String s = "hello";
System.out.println(s.length());
// char charAt(int index):返回字符串中指定索引处的字符。
System.out.println(s.charAt(4));
// int indexOf(int ch):返回指定的字符在字符串中第一次出现的索引。
System.out.println(s.indexOf('l'));
// int indexOf(String str):返回指定的字符串在字符串中第一次出现的索引。
System.out.println(s.indexOf("llo"));
// String substring(int start):截取从start开始到末尾的字符串。
System.out.println(s.substring(4));
// String substring(int start,int end):截取从start开始到end结束的字符串。
System.out.println(s.substring(2, 5));
//这里的开始和结束的索引值,全部都是包前不包后
}
}
C:转换功能
byte[] getBytes():把字符串转换为字节数组
char[] toCharArray():把字符串转换为字符数组
static String valueOf(char[] chs):把字符数组转换为字符串
static String valueOf(int i):把int类型的数据转换为字符串
valueOf():可以把任意类型的数据转换为字符串。
String toLowerCase():把字符串转成小写
String toUpperCase():把字符串转成大写
String concat(String str):拼接字符串,前面我们使用过+进行字符串的拼接,不够专业。
D:其他功能
A:替换功能
String replace(char old,char new)
String replace(String old,String new)
B:去除字符串两端空格
String trim()
package day4.edu_10;
/**
C:转换功能
byte[] getBytes():把字符串转换为字节数组
char[] toCharArray():把字符串转换为字符数组
static String valueOf(char[] chs):把字符数组转换为字符串
static String valueOf(int i):把int类型的数据转换为字符串
valueOf():可以把任意类型的数据转换为字符串。
String toLowerCase():把字符串转成小写
String toUpperCase():把字符串转成大写
String concat(String str):拼接字符串,前面我们使用过+进行字符串的拼接,不够专业。
*
D:其他功能
A:替换功能
String replace(char old,char new)
String replace(String old,String new)
B:去除字符串两端空格
String trim()
*
*/
public class StringDemo7 {
public static void main(String[] args) {
String s = "hello";
// byte[] getBytes():把字符串转换为字节数组
byte[] bys = s.getBytes();
// System.out.println(bys);
// char[] toCharArray():把字符串转换为字符数组
char[] chs = s.toCharArray();
for(int i=0;i<5;i++){
System.out.println(chs[i]);
}
// static String valueOf(char[] chs):把字符数组转换为字符串
char[] chs2 = {'a','s','d'};
String s2 = String.valueOf(chs2);
System.out.println(s2);
// static String valueOf(int i):把int类型的数据转换为字符串
String s3 = String.valueOf(12.32);
System.out.println(s3);
// valueOf():可以把任意类型的数据转换为字符串。
// String toLowerCase():把字符串转成小写
String s5 = "HELLoWoRLD";
System.out.println(s5.toLowerCase());
// String toUpperCase():把字符串转成大写
System.out.println(s5.toUpperCase());
// String concat(String str):拼接字符串,前面我们使用过+进行字符串的拼接,不够专业。
String s6 = "hello";
String s7 = "world";
System.out.println(s6.concat(s7));
// String replace(char old,char new)
System.out.println(s6.replace('e', 'a'));
// String replace(String old,String new)
System.out.println(s6.replace("hel", "hal"));
// String trim()
String s8 = " fdv gdf gds ";
System.out.println(s8);
System.out.println(s8.trim());
}
}
练习:转换大小写及拼接
package com.edu_10;
public class StringDemo8 {
public static void main(String[] args) {
//"HElloWorld"
String s = "HElloWorld" ;
String s2 = s.substring(0, 5);
String s3 = s.substring(5);
//转换大小写带拼接
String s4 = s2.toUpperCase().concat(s3.toLowerCase());
System.out.println(s4);
}
}
十一:StringBuffer和StringBuilder
11.1
* StringBuffer:
* 线程安全的可变字符序列。
*
* String和StringBuffer的区别?
* A:String的内容不可变
* B:StringBuffer的内容可变
*
* StringBuffer和StringBuilder的区别?
* A:StringBuffer 线程安全,效率低
* B:StringBuilder 线程不安全,效率高
*
* 线程安全:(同步),效率低
* 线程不安全:(不同步),效率高
*
* 构造方法:
* StringBuffer():构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。
* StringBuffer(int capacity):构造一个其中不带字符的字符串缓冲区,其初始容量为 capacity个字符。
* StringBuffer(String str):构造一个其中带字符的字符串缓冲区,其初始容量为??? 个字符。
package day4.edu_11;
public class StringBufferDemo1 {
public static void main(String[] args) {
//StringBuffer():构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。
StringBuffer sb = new StringBuffer();
System.out.println(sb.length());//实际容量
System.out.println(sb.capacity());//理论容量
System.out.println("-----------------------");
//StringBuffer(int capacity):构造一个其中不带字符的字符串缓冲区,其初始容量为 capacity个字符。
StringBuffer sb2 = new StringBuffer(10);
System.out.println(sb2.length());
System.out.println(sb2.capacity());
System.out.println("---------------------");
//StringBuffer(String str):构造一个其中带字符的字符串缓冲区,其初始容量为??? 个字符。
StringBuffer sb3 = new StringBuffer("hello");
System.out.println(sb3.capacity());
System.out.println(sb3.length());
}
}
- 成员方法:
- public int length():返回长度(字符数)。实际值
- public int capacity():返回当前容量。 理论值
- 添加功能:添加元素,并返回本身的对象。
- A:public StringBuffer append(String str):追加数据,在末尾添加
- B:public StringBuffer insert(int offset,String str):插入数据,在指定位置添加
package day4.edu_11;
public class StringBufferDemo2 {
public static void main(String[] args) {
//A:public StringBuffer append(String str):追加数据,在末尾添加
StringBuffer s = new StringBuffer();
s.append("hello");
s.append("world");
System.out.println(s);
s.append(true).append("hello").append("java").append(100);
System.out.println(s);
//B:public StringBuffer insert(int offset,String str):插入数据,在指定位置添加
//sb.insert(0, "false");
s.insert(4, "fasle");
System.out.println(s);
}
}
- 删除功能:
- public StringBuffer deleteCharAt(int index):删除指定索引处的字符
- public StringBuffer delete(int start,int end):删除从start开始到end结束的数据,包左不包右
package day4.edu_11;
public class StringBufferDemo3 {
public static void main(String[] args) {
//public StringBuffer deleteCharAt(int index):删除指定索引处的字符
StringBuffer sb =new StringBuffer("hello");
sb.deleteCharAt(1);
System.out.println(sb);
//public StringBuffer delete(int start,int end):
//删除从start开始到end结束的数据,包左不包右
sb.delete(1, 3);
System.out.println(sb);
}
}
- 替换功能:
- public StringBuffer replace(int start,int end,String str):用str替换从start到end的数据
-
- 反转功能:
- public StringBuffer reverse()
- 截取功能:返回值类型是String类型,本身没有发生改变
- public String substring(int start)
- public String substring(int start,int end)
package day4.edu_11;
public class StringBufferDemo4 {
public static void main(String[] args) {
//替换功能:public StringBuffer replace(int start,int end,String str):
//用str替换从start到end的数据
StringBuffer sb = new StringBuffer("hello");
sb.replace(1, 3, "ab");
System.out.println(sb);
System.out.println("-----------------------");
//反转功能:public StringBuffer reverse()
StringBuffer sb1 = new StringBuffer("hello");
sb1.reverse();
System.out.println(sb1);
System.out.println("-------------------");
// 截取功能:返回值类型是String类型,本身没有发生改变
// public String substring(int start)
// public String substring(int start,int end)
StringBuffer sb2 = new StringBuffer("hello");
String s = sb2.substring(2);
System.out.println(s);
String s1 = sb2.substring(1, 4);
System.out.println(s1);
}
}
- 相互转换:
-
- String – StringBuffer
- String s = “hello”;
// 方式1
StringBuffer sb1 = new StringBuffer(s);
// 方式2
StringBuffer sb2 = new StringBuffer();
sb2.append(s); -
- StringBuffer – String
- StringBuffer sb = new StringBuffer(“world”);
//方式1
String s1 = sb.substring(0);
//方式2
String s2 = sb.toString();
//方式3
String s3 = new String(sb);