课堂知识点
一、API
1.帮助文档使用步骤
略~
二、String类
1.String类介绍
String类代表的是字符串的对象 String字符串内容不可改变 虽然内容不可改变,但是可以被共享使用
2.String类构造方法
方法名 说明 String() 创建一个内容为空的字符串对象 String(char[] arr) 根据字符数组创建一个字符串对象 String(byte[] arr) 根据字节数组创建一个字符串对象 String s = “abc”; 快速定义字符串对象的方式
public class StringDemo01 {
public static void main ( String [ ] args) {
String s1 = new String ( ) ;
System . out. println ( "s1:" + s1) ;
char [ ] chs = { 'a' , 'b' , 'c' } ;
String s2 = new String ( chs) ;
System . out. println ( "s2:" + s2) ;
byte [ ] bys = { 97 , 98 , 99 } ;
String s3 = new String ( bys) ;
System . out. println ( "s3:" + s3) ;
String s4 = "abc" ;
System . out. println ( "s4:" + s4) ;
}
}
3.String类的特点
如果是new的方式创建字符串对象,保存的是堆内存中的对象的地址值 如果是直接赋值的方式创建字符串对象,保存的是常量池中的对象的地址值 图解
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kkeD0ygD-1627951199587)(img\01.png)]
4.String类的成员方法-equals()
==号
如果比较的基本数据类型,比较的是具体的值 如果比较的是引用数据类型,比较的是地址值 equals方法:比较字符串的内容是否相同,区分大小写 (PS:构造方法 String s=new String(),()中只允许写一个字符串吗?怎么做才可以写多个?)
public class Demo01 {
public static void main ( String [ ] args) {
String s1 = new String ( "abc" ) ;
String s2 = new String ( "abc" ) ;
System . out. println ( s1 == s2) ;
String s3 = "abc" ;
String s4 = "abc" ;
System . out. println ( s3 == s4) ;
System . out. println ( s1 == s3) ;
System . out. println ( "----------------" ) ;
System . out. println ( s1. equals ( s2) ) ;
System . out. println ( s3. equals ( s4) ) ;
System . out. println ( s1. equals ( s3) ) ;
System . out. println ( "-----------------" ) ;
String s5 = "ABC" ;
System . out. println ( s4. equals ( s5) ) ;
}
}
5.String类案例-用户登录
public class StringTest01 {
public static void main ( String [ ] args) {
String username = "itheima" ;
String password = "czbk" ;
Scanner sc = new Scanner ( System . in) ;
for ( int i= 0 ; i< 3 ; i++ ) {
System . out. println ( "请输入用户名:" ) ;
String name = sc. nextLine ( ) ;
System . out. println ( "请输入密码:" ) ;
String pwd = sc. nextLine ( ) ;
if ( name. equals ( username) && pwd. equals ( password) ) {
System . out. println ( "登录成功" ) ;
break ;
} else {
if ( 2 - i == 0 ) {
System . out. println ( "你的账户被锁定,请与管理员联系" ) ;
} else {
System . out. println ( "登录失败,你还有" + ( 2 - i) + "次机会" ) ;
}
}
}
}
}
6.String类的成员方法-charAt()和length()
char charAt(int index) 获取指定索引上的字符 int length() 获取字符串的长度 遍历字符串
public class StringTest02 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入一个字符串:" ) ;
String line = sc. nextLine ( ) ;
for ( int i = 0 ; i < line. length ( ) ; i++ ) {
System . out. println ( line. charAt ( i) ) ;
}
}
}
7.String类案例-统计字符个数
public class StringTest03 {
public static void main ( String [ ] args) {
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "请输入一个字符串:" ) ;
String line = sc. nextLine ( ) ;
int bigCount = 0 ;
int smallCount = 0 ;
int numberCount = 0 ;
int otherCount = 0 ;
for ( int i= 0 ; i< line. length ( ) ; i++ ) {
char ch = line. charAt ( i) ;
if ( ch>= 'A' && ch<= 'Z' ) {
bigCount++ ;
} else if ( ch>= 'a' && ch<= 'z' ) {
smallCount++ ;
} else if ( ch>= '0' && ch<= '9' ) {
numberCount++ ;
} else {
otherCount++ ;
}
}
System . out. println ( "大写字母:" + bigCount + "个" ) ;
System . out. println ( "小写字母:" + smallCount + "个" ) ;
System . out. println ( "数字:" + numberCount + "个" ) ;
System . out. println ( "其他字符:" + otherCount + "个" ) ;
}
}
8.String类案例-拼接数组元素
public class StringTest04 {
public static void main ( String [ ] args) {
int [ ] arr = { 1 , 2 , 3 } ;
String s = arrayToString ( arr) ;
System . out. println ( "s:" + s) ;
}
public static String arrayToString ( int [ ] arr) {
String s = "" ;
s += "[" ;
for ( int i= 0 ; i< arr. length; i++ ) {
if ( i== arr. length- 1 ) {
s += arr[ i] ;
} else {
s += arr[ i] ;
s += ", " ;
}
}
s += "]" ;
return s;
}
}
9.String类案例-字符串反转
public static void reverse ( String s) {
String ss = "" ;
for ( int i = s. length ( ) - 1 ; i >= 0 ; i-- ) {
char ch = s. charAt ( i) ;
ss += ch;
}
System . out. println ( ss) ;
}
三、StringBuilder类
1.StringBuilder类概念
StringBuilder是一个可变的字符序列 来自于java.lang包,所以使用时不需要导包
2.StringBuilder构造方法
方法名 说明 StringBuilder() 创建一个内容为空的字符串缓冲区对象 StringBuilder(String str) 根据字符串的内容来创建字符串缓冲区对象
public class StringBuilderDemo01 {
public static void main ( String [ ] args) {
StringBuilder sb = new StringBuilder ( ) ;
System . out. println ( "sb:" + sb) ;
System . out. println ( "sb.length():" + sb. length ( ) ) ;
StringBuilder sb2 = new StringBuilder ( "hello" ) ;
System . out. println ( "sb2:" + sb2) ;
System . out. println ( "sb2.length():" + sb2. length ( ) ) ;
}
}
3.StringBuilder成员方法
方法名 说明 StringBuilder append(任意类型); 向缓冲区中追加数据 StringBuilder reverse(); 将缓冲区的内容反转 String toString(); 将缓冲区的内容转成字符串 int length(); 获取缓冲区的长度
public class StringBuilderDemo01 {
public static void main ( String [ ] args) {
StringBuilder sb = new StringBuilder ( ) ;
sb. append ( "hello" ) . append ( "world" ) . append ( "java" ) . append ( 100 ) ;
System . out. println ( "sb:" + sb) ;
sb. reverse ( ) ;
System . out. println ( "sb:" + sb) ;
System . out. println ( sb. length ( ) ) ;
String str = sb. toString ( ) ;
System . out. println ( str) ;
}
}
4.StringBuilder和String相互转换
String转成StringBuilder StringBuilder转成String 示例代码
public class Demo03 {
public static void main ( String [ ] args) {
String s1 = "hello" ;
StringBuilder sb = new StringBuilder ( ) ;
sb. append ( s1) ;
System . out. println ( sb) ;
System . out. println ( "-------------" ) ;
StringBuilder sb2 = new StringBuilder ( ) ;
sb2. append ( "hello" ) . append ( "," ) . append ( "world" ) . append ( "," ) . append ( "java" ) ;
System . out. println ( sb2) ;
String ss = sb2. toString ( ) ;
System . out. println ( "ss:" + ss) ;
}
}
5.StringBuilder案例-数组元素拼接
public class StringBuilderTest01 {
public static void main ( String [ ] args) {
int [ ] arr = { 1 , 2 , 3 } ;
String s = arrayToString ( arr) ;
System . out. println ( "s:" + s) ;
}
public static String arrayToString ( int [ ] arr) {
StringBuilder sb = new StringBuilder ( ) ;
sb. append ( "[" ) ;
for ( int i= 0 ; i< arr. length; i++ ) {
if ( i == arr. length- 1 ) {
sb. append ( arr[ i] ) ;
} else {
sb. append ( arr[ i] ) . append ( ", " ) ;
}
}
sb. append ( "]" ) ;
String s = sb. toString ( ) ;
return s;
}
}
6.StringBuilder案例-对称字符串
public class Demo04 {
public static void main ( String [ ] args) {
String [ ] arr = { "aa" , "aba" , "abcd" , "大王天天王大" , "上海自来水来自海上" } ;
print ( arr) ;
}
public static void print ( String [ ] arr) {
for ( int i = 0 ; i < arr. length; i++ ) {
String str = arr[ i] ;
StringBuilder sb = new StringBuilder ( str) ;
sb. reverse ( ) ;
String rev = sb. toString ( ) ;
if ( rev. equals ( str) ) {
System . out. println ( str + "是对称的" ) ;
} else {
System . out. println ( str + "不是对称的" ) ;
}
}
}
}
练习题
过一遍笔记 √ 练习使用API帮助文档的查看 √ 练习String类的构造方法的使用 √ 练习String类的成员方法的使用-equals()-charAt()-length() √ 练习String类的案例 √ 练习StringBuilder类的构造方法的使用 √ 练习StringBuilder类的成员方法的使用-append()-reverse()-toString()-length() √ 练习StringBuilder和String之间相互转换 √ 练习StringBuilder类的案例
面试题
String类有什么特点 String类通过构造方法创建对象和直接赋值有什么区别 ==号有什么特点 StringBuilder类有什么特点 str + “不是对称的”); } } } }
## 练习题
1. 过一遍笔记 √
2. 练习使用API帮助文档的查看 √
3. 练习String类的构造方法的使用 √
4. 练习String类的成员方法的使用-equals()-charAt()-length() √
5. 练习String类的案例 √
6. 练习StringBuilder类的构造方法的使用 √
7. 练习StringBuilder类的成员方法的使用-append()-reverse()-toString()-length() √
8. 练习StringBuilder和String之间相互转换 √
9. 练习StringBuilder类的案例
## 面试题
- String类有什么特点
- String类通过构造方法创建对象和直接赋值有什么区别
- ==号有什么特点
- StringBuilder类有什么特点
- StringBuilder和String如何相互转换