==是进行对象的地址值比较,如果确实需要字符串内容比较,可以使用如下方法:
public boolean equals(Object obj):参数可以是任何对象,只有参数是一个字符串并且内容相同才会为true,否则返回false
equals方法注意事项:
任何对象都能使用Object进行接收
equals方法具有对称性,也就是a.equals(b)和b.equals(a)效果一样
如果比较双放一个常量一个变量,推荐把常量字符串写在前面
推荐:"abc".equals(str) 不推荐:str.equals("abc")
public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写
3、能够使用文档查询String类的获取方法
public int length():获取字符串当中含有的字符个数,拿到字符串长度
public String concat(String str):将指定的字符串连接到该字符串的末尾,返回新的字符串
public char charAt(int index):获取指定索引位置的单个字符(索引从0开始)
public in indexOf(Stirng str):查找参数字符串在本字符串当中首次出现的索引位置,如果没有,返回-1值
public Stirng substring(int index):截取从参数位置一直到字符串末尾,返回新字符串
public String substring(int begain,in end):截取从begain开始,一直到end结束,中间的字符串。注意:[begain,end),左闭右开区间
字符串的分割方法:
public Stirng[] split(String regex):按照参数的规则,将字符串切分成为若干部分
注意事项:
split方法的参数其实是一个“正则表达式”,今天要注意,如果按照引文据点“.”进行切分,必须写“\\.”才能切分成功
4、能够使用文档查询String类的转换方法
public char[] toCharArray():将当前字符串拆分成为字符数组作为返回值
public byte[] getBytes():把字符串转换为字节数组
public String replace(CharSequence oldStirng,CharSequence new String);
将所有出现的老字符串替换成为新的字符串,返回替换之后的结果新字符串
CharSequence意思就是可以接收字符串类型
public class Student {
private int id;
private String name;
private int age;
static String room;//被static修饰,所有对象共享共一份,所有学生都在同一间教室
private static int idCount = 0;//学号计数器,每当new了一个新对象,计数器自增1
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
this.id = ++idCount;//由于学号要求new对象时自动增加1,所有不能作为参数写在Student构造方法参数列表中
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.itheima.test01;
import static com.itheima.test01.Student.room;
public class StaticStudentTest01 {
public static void main(String[] args) {
//创建学生对象
Student sd1 = new Student("赵丽颖",29);
//给学生对象设置教室,因room被static修饰,所以只创建一次,其他学生对象直接调用即可
Student.room = "804教室";
Student sd2 = new Student("唐嫣",30);