一、API帮助文档的使用+java中的常用的一些包的使用
二、String类讲解
注意:是 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():返回此字符串的长度
(3)String的特点及例题
A:String类的数据特点:字符串是常量;它们的值在创建之后不能更改
package com.edu_10;
public class StringDemo2 {
public static void main(String[] args) {
String s = "hello";
s += "world";
System.out.println(s);
}
}
//helloworld
结论:字符串的内容不能发生改变,但是字符串引用的指向是可以改变的。
B:String s = new String(“hello”)和String s = “hello”的区别:前者创建了1个或者2个对象,后缀创建了0个或者1个对象
C:new和直接赋值的区别
package com.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
}
}
D:变量相加和常量相加的区别
package com.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 + s2); //fasle
System.out.println(s3.equals(s1 + s2)); //true
System.out.println(s3 == "hello" + "world"); //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 com.edu_10;
public class StringDemo5 {
public static void main(String[] args) {
String s = "hello";
String s2 = "Hello";
System.out.println(s.equals(s2));//false
System.out.println(s.equalsIgnoreCase(s2));//true
System.out.println(s.contains("hel"));//true
System.out.println(s.startsWith("h"));//true
System.out.println(s.endsWith("lo"));//true
//boolean isEmpty():判断字符串的内容是否为空
//String s3 = null;
String s3 = "";//true
/**
* java.lang.NullPointerException:空指针异常
* 对象为空:null,说明这个引用没有指向
* 字符串为空:就是一个空字符串,字符串中什么也没有
*/
System.out.println(s3.isEmpty());
}
}
B:获取功能
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 com.edu_10;
public class StringDemo6 {
public static void main(String[] args) {
String s = "hello";
System.out.println(s.length());//5
System.out.println(s.charAt(0));//h
System.out.println(s.indexOf('l'));//2
String s2 = s.substring(2);
System.out.println(s2);//llo
System.out.println(s.substring(0, 3));//hel
//这里的开始和结束的索引值,全部都是包前不包后
}
}
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 com.edu_10;
public class StringDemo8 {
public static void main(String[] args) {
//做一个练习
//把"HElloWorld"前5个字母全部大写,后5个字母全部小写
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
StringBuffer:线程安全的可变字符序列。
String和StringBuffer的区别?
A:String的内容不可变
B:StringBuffer的内容可变
StringBuffer和StringBuilder的区别?
A:StringBuffer 线程安全,效率低
B:StringBuilder 线程不安全,效率高
线程安全:(同步),效率低;线程不安全:(不同步),效率高
构造方法:
StringBuffer():构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。
StringBuffer(int capacity):构造一个其中不带字符的字符串缓冲区,其初始容量为 capacity个字符。
StringBuffer(String str):构造一个其中带字符的字符串缓冲区,其初始容量为??? 个字符。
成员方法:
public int length():返回长度(字符数)。实际值
public int capacity():返回当前容量。 理论值
package com.edu_11;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println(sb.length());//实际容量
System.out.println(sb.capacity());//理论容量
System.out.println("-----------------------");
StringBuffer sb2 = new StringBuffer(10);
System.out.println(sb2.length());
System.out.println(sb2.capacity());
System.out.println("---------------------");
StringBuffer sb3 = new StringBuffer("hello");
System.out.println(sb3);
}
}
// 0
// 16
// -----------------------
// 0
// 10
// ---------------------
// hello
添加功能:添加元素,并返回本身的对象。
public StringBuffer append(String str):追加数据,在末尾添加
public StringBuffer insert(int offset,String str):插入数据,在指定位置添加
package com.edu_11;
public class StringBufferDemo2 {
public static void main(String[] args) {
StringBuffer sb =new StringBuffer();
sb.append("hello");
sb.append(100);
sb.append(true);
sb.append('a');
System.out.println(sb);
sb.append(true).append("hello").append("java").append(100);
System.out.println(sb);
sb.insert(4, "fasle");
System.out.println(sb);
}
}
// hello100truea
// hello100trueatruehellojava100
// hellfasleo100trueatruehellojava100
删除功能:
public StringBuffer deleteCharAt(int index):删除指定索引处的字符
public StringBuffer delete(int start,int end):删除从start开始到end结束的数据,包左不包右
package com.edu_11;
public class StringBufferDemo3 {
public static void main(String[] args) {
StringBuffer sb =new StringBuffer("hello");
sb.deleteCharAt(1);
System.out.println(sb);
sb.delete(1, 3);
System.out.println(sb);
}
}
//hllo
//ho
替换功能:
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)
相互转换:
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);