1.String类
String类的概述和使用(掌握)
A:多个字符组成的一串数据
其实它可以和字符数组进行相互转换。
B:构造方法:
1. public String()
2.public String(byte[] bytes)
3.public String(byte[] bytes,int offset,int length)
4.public String(char[] value)
5.public String(char[] value,int offset,int count)
6.public String(String original)
下面这个虽然不是构造方法,但结果也是一个字符串对象
7.String s = “hello”;
C:字符串的特点
1.字符串一旦被赋值,就能改变
注意:这里指的是字符串的内容不能改变,而不是引用不能改变
2.字面值作为字符串对象和通过构造方法创建对象的不同
String s = new String(“hello”);和String s = “hello”;的区别?
方法区和堆都有 在方法区存在
D:字符串的面试题(看程序写结果)
1、==和equals()
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
2、字符串
* 字符串如果是变量相加,先开空间,在拼接
* 字符串如果是常量相加,是先加,然后在常量池里找,如果有就返回,否则,就创建。
*
* 看不明白的时候可以反编译
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1+s2);//false
System.out.println(s3.equals(s1+s2));//true
System.out.println(s3 == "hello" + "world");//true
System.out.println(s3.equals("hello"+"world"));//true
//通过反编译看源码这里已经做好了处理
System.out.println(s3 == "helloworld");
System.out.println(s3.equals("helloworld"));
E:字符串的功能
1.判断功能
String的判断功能
boolean equals(Object obj);//比较字符串的内容是否相同,区分大小写
boolean equalsIgnoreCase(String str);//比较字符串的内容是否相同,忽略大小写
boolean contains(String str);//判断大字符串中是否包含小字符串
boolean statsWith(String str);//判断字符串是否以某个指定的字符串开头
boolean endsWith(String str);//判断字符串是否以某个指定的字符串结尾
boolean isEmpty();//判断字符串是否为空
当且仅当 length() 为 0 时返回 true
2.获取功能
String类的获取功能 前缀都是public
int length();//获取字符串的长度
char charAt(int index);获取指定位置的字符
int indexOf(int ch); 返回指定字符在此字符串中第一次出现处的索引。
为什么这里是int类型,而不是char类型?
原因:'a'和97其实都可以代表'a'.
int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引。
int indexOf(int ch,int formIndex);返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
int indexOf(String str,int formIndex); 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
String substring(int start);从指定位置开始截取字符串,默认到末尾。
String substring(int start,int end);从指定位置开始到指定位置结束,截取字符串。
3.转换功能
byte[] getBytes()把字符串转换成字节数组
char[] toCharArray()把字符串转换成字符数组
static String valueOf(char[] chs)把字符数组转换成字符串
static String valueOf(int i)把int类型转换成字符串
String toLowerCase()把字符串转换成小写
String toUpperCase()把字符串转换成大写
String concat(String str)把字符串拼接
4.其他功能
A:替换功能
String replace(char old,char new)
String replace(String old, String new)
B:去空格功能
String trim()去两边的空格
C:按字典比较功能
int compareTo(String str)
int compareToIgnoreCase(String str)
2.StringBuffer的基本使用
StringBuffer(掌握)
(1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又比较常见的,为了解决这个问题,
java就提供了一个字符串缓冲区类。StringBuffer供我们使用
(2)StringBuffer的构造方法
A:StringBuffer()
B:StringBuffer(int size)//指定容量的字符串缓冲区对象
C:StringBuffer(String str)指定内容的字符串缓冲区对象
StringBuffer的方法
public int capacity();返回当前容量,理论值
public int length();返回长度(字符数),实际值
(3)StringBuffer的常见功能(自己补齐)
A:添加功能
public StringBuffer append(String str):可以把任意类型添加到字符串缓冲区里面
返回StringBuffer本身,没有创建新的对象
public StringBuffer insert(int offset,String str);在指定位置吧任意类型的字符串插入到字符串缓冲区,
并返回字符串缓冲区本身从offset插入str字符
B:删除功能
public StringBuffer deleteCharAt(int index)删除指定位置的字符,并返回本身
public StringBuffer delete(int start,int end)删除从指定位置开始指定位置结束的内容,并返回本身
包左不包右
C:替换功能
public StringBuffer replace (int start,int end,String str):
从start开始到end结束用str替换。包左不包右
D:反转功能
public StringBuffer reverse();
对象名.reverse()方法。
E:截取功能(注意这个返回值String)
public String substring(int start)从start开始截取返回新的String对象
public String substring(int start,int end)包左不包右
(4)StringBuffer的练习
//注意:不能把字符串的值直接赋值给StringBuffer
A:String和StringBuffer相互转换
String -- StringBuffer
String s = "hello";
StringBuffer sb = new StringBuffer(s);
通过构造方法
append()方法。
StringBuffer sb2 = new StringBuffer();
sb2.append(s);
StringBuffer--String
StringBuffer buffer = new StringBuffer("java");
String str = new String(buffer);
toString()方法
String str2 = buffer.toString();
B:字符串拼接
cn.itcast_07
C:把字符串反转
1.先把字符串转换为字符数组在倒序输出
2.把字符串转换为StringBuffer使用reverse()功能
再用toString转换为字符串输出
D:判断一个字符串是否对称
public static boolean isSame(String s){
//把字符串转换成字符数组
char[] chs = s.toCharArray();
for (int start = 0, end = chs.length-1; start <= end; start++,end--) {
if(chs[start] != chs[end]){
return false;
}
}
return true;
}
//也可以用字符串反转再equals对比原字符串返回boolean
StringBuffer(s).reverse().toString().equals(s);
(5)面试题
小细节:
StringBuffer:同步的,数据安全,效率低
StringBuilder:不同步的,数据不安全,效率高
A:String,StringBuffer,StringBuilder的区别
String内容长度是不可变的,StringBuffer和StringBuilder是可变的
如果使用StringBuilder和StringBuffer做字符串拼接,不会浪费太多资源
B:StringBuffer和数组的区别?
相同:都可以看成是容器,装其他数据
不同:StringBuffer什么类型都可以最终是一个字符串数据
数组可以放置多种一种数据类型的,但必须是同一种数据类型
(6)注意问题:
String作为形式参数,StringBuffer作为形式参数
3.时间类型Date
Date/Format
(1)Date 是日期类,可以精确到毫秒
A:构造方法:
Date()
Date(long time)
B:成员方法
getTime()
setTime(long time)
C:日期和毫秒值的相互转换
(2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类
A:SimpleDateFormat() 默认模式
B:SimpleDateFormat(String patten) 给定模式
常用格式:yyyy-MM-dd HH:mm:ss
C:日期和字符串的转换
a:Date -- String
format()
b:String -- Date
parse()
public class DateFormatDemo {
public static void main(String[] args) throws ParseException {
//Date -- String
//创建Date对象
Date d = new Date();
//创建时间格式化对象
SimpleDateFormat sdf = new SimpleDateFormat();
String s = sdf.format(d);
System.out.println(s);
//指定格式的格式化
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String ss = sdf1.format(d);
System.out.println(ss);
// Strig -- Date
String s1 = "2012-08-08 20:08:08";
// 在把一个字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//public Date parse(String source) 解析
Date d2 = sdf2.parse(s1);
System.out.println(d2);
}
}