构造方法
在创建对象实例时,由于经常要同时初始化这个实例的字段,那么就需要使用构造方法,来初始化实例。
构造方法分为无参构造方法和有参构造方法,在编译器中,如果没有定义构造方法,则编译器会自动定义一个无参构造方法:访问修饰符+类名,它没有参数也没有执行语句。
如果在实例化对象时,需要传入参数,则需要通过有参构造方法:访问修饰符+类名(传入的参数){执行语句}。
注意:构造方法没有返回值。
String类中的构造方法
// 字符串常量池
String s0 = "";
// 无参构造,创建一个空字符
String s1 = new String();
// 有参构造,创建字符串c
String s2 = new String("c");
// 使用char[]数组创建
String s3 = new String(new char[] {'H','e','l','l','0'});
// 使用gbk编码集创建
String s4 = new String(new byte[] {-28,-72,-83},"gbk");
静态方法
用static关键字修饰的方法称为静态方法。调用实例方法必须通过一个实例对象,而调用静态方法则不需要实例对象,通过类名就可以调用。
静态方法只能访问静态成员变量或方法,不能访问普通方法和普通成员变量。但是普通方法可以访问静态成员变量和静态方法。
静态方法由于可以通过类名直接调用,所以可以没有实例化对象,因此静态方法无法使用this关键字,也无法访问实例对象的成员变量。
public class Demo{
public static void main(String[] args){
Employee.setAge(15); // 通过类名可以访问静态成员变量
// Employee.setName("李四"); // 编译报错,通过类名不可以访问普通成员变量
// Employee.work(); // 类名不能直接访问普通方法
Employee.dosth(); // 类名可以直接访问静态方法
Employee emp = new Employee(); // 无参构造方法创建实例化对象
emp.setName("张三"); // 实例化对象 访问普通成员变量
emp.setAge(15); // 实例化对象 访问静态成员变量
emp.work(); // 实例化对象 调用普通方法
emp.dosth(); // 实例化对象 调用静态方法
}
}
class Employee{
private String name;
private static int age;
// 普通方法
public void work() {
System.out.println(this.name); // 允许访问普通成员变量
System.out.println(this.age); // 允许访问静态成员变量
study();// 允许访问静态方法
System.out.println("Employee类的work()方法");
}
// 静态方法
public static void dosth() {
// this.name = "李四"; // 编译报错,不允许访问普通成员变量
// this.age // 编译报错,不能使用this关键字
System.out.println(age); // 可以访问静态成员变量
System.out.println("Employee类的dosth()方法");
// work();// 编译报错,不允许访问普通方法
}
// 静态方法
public static void study() {
System.out.println("Employee类的study()方法");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static int getAge() {
return age;
}
public static void setAge(int age) {
Employee.age = age;
}
}
String类中静态方法
拼接字符串
使用静态方法join(),它用指定的字符串连接字符串数组
String str1 = "北京", str2 = "杭州", str3 = "上海";
String result = String.join("#", str1, str2, str3);
System.out.println(result); // 北京#杭州#上海
String[] cityArrays = { "北京", "郑州", "大连", "深圳", "天津" };
String result2 = String.join("^_^", cityArrays);
System.out.println(result2); // 北京^_^郑州^_^大连^_^深圳^_^天津
格式化字符串
使用format()静态方法,可以传入其他参数,替换占位符,然后生成新的字符串
String str1 = "北京", str2 = "杭州", str3 = "上海";
String result4 = String.format("城市1:%s\n城市2:%s\n城市3:%s\n", str1, str2, str3);
System.out.println(result4); // 城市1:北京 城市2:杭州 城市3:上海
类型转换
要把任意基本类型或引用类型转换为字符串,可以使用静态方法valueOf()。
int number = 23944;
// 不推荐
// String strNum = number +""; // 任意类型与字符串类型拼接,结果都为字符串
// 推荐
String strNum = String.valueOf(number); // 使用String类的valueOf()方法进行转换
要把字符串转成其他类型使用对应包装类的的方法
String number = "3.14";
Double result = Double.parseDouble(number) - 1;
System.out.println(result); // 2.14
String类中普通方法
字符串比较
当想比较两个字符串是否相同时,我们通常会使用equals()方法而不能使用==关系运算符,该方法返回值是布尔类型,相同为true,不同为false。
String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s3)); // true
可以看到使用equals()的结果都为true,但是使用"==" 的结果不同,原因是==判断引用数据类型的是地址,由于s1和s2在字符串常量池中指向同一个地址,结果为true,但是由于s3新开辟了一块内存地址,结果为false
字符串搜索
使用indexOf():查找子字符串第一次出现的下标位置,若没出现,则返回-1。
使用lastIndexOf() : 查找子字符串最后一次出现的下标位置,若没出现,则返回-1。
使用startsWith()和endsWith()方法是用于判断字符串是否以指定字符串开头或结尾,返回值为boolean类型。
使用contains()方法用于查找当前字符串中是否存在指定子字符串,返回值为boolean类型。
public class Demo {
public static void main(String[] args) {
String s1= "just do IT do myself do";
// indexOf():查找子字符串第一次出现的下标位置
// 当前字符串:indexOf("子字符串")
// 如果存在,则返回子字符串的下标
int v1 = s1.indexOf("do"); // 从下标为0的位置开始查找“do”
System.out.println(v1); // 5
// 如果不存在,则返回-1
int v2 = s1.indexOf("wo"); // 从下标为0的位置开始查找“wo”
System.out.println(v2); // -1
// lastIndexOf() : 查找子字符串最后一次出现的下标位置
int v3 = s1.lastIndexOf("do"); // 从尾下标位置开始查找“do”
System.out.println(v3); // 21
int v4 = s1.lastIndexOf("wo"); // 从尾下标的位置开始查找“wo”
System.out.println(v4); // -1
// 从指定位置开始查找子字符串
int v5 = s1.indexOf("do",v1+1); // 从下标为"v1+1"的位置开始查找“do”
System.out.println(v5);// 11
// startsWith():查找子字符串是否以指定字符串开始
boolean isStart = s1.startsWith("just");
System.out.println(isStart); // true
// endsWith():查找子字符串是否以指定字符串结束
boolean isEnd = s1.endsWith("do");
System.out.println(isEnd); // true
//contains():查找字符串是否存在指定字符串
boolean isCon = s1.contains("just");
System.out.println(isCon); // true
}
}
截取子字符串
使用subString()可以截取从指定下标开始或截取从指定下标开始,截取到指定下标的字符串
String str = "风急天高猿啸哀";
System.out.println(str.substring(2)); // 天高猿啸哀
System.out.println(str.substring(2, 4)); // 天高
去除首尾空白字符
使用trim()方法可以移除字符串首尾空白字符。空白字符包括空格,\t,\r,\n:
String line = "\t\tabc\n\t";
// 去除字符串“两端”空格换行(空白字符)
line = line.trim();
System.out.println(line); // abc
System.out.println(line.length()); // 3
替换字符串
使用replace()方法可以根据字符或字符串替换,也可以根据正则表达式替换
String str = "渭城朝雨浥轻尘,客舍青青柳色新。";
// 字符替换字符
// 写法1
// str = str.replace('雨', '雾');
// str = str.replace('青', '绿');
// 写法2
str = str.replace('雨', '雾').replace('青', '绿');
System.out.println(str); // 渭城朝雾浥轻尘,客舍绿绿柳色新。
// 字符串替换字符串
str = str.replace("渭城", "渭城区");
System.out.println(str); // 渭城区朝雾浥轻尘,客舍绿绿柳色新。
str = "\"You\" (or \"Your\") sha1ll mean; an individu2al,
// 正则表达式
str = str.replaceAll("[\"\\,\\.\\;]", "") // 替换标点符号
.replaceAll("[0-9]",""); // 替换所有的数字
System.out.println(str); // You (or Your) shall mean an individual
分割字符串
使用split()方法,传入正则表达式
String line = "北京.上海.广州.深圳.成都";
// 切割字符串
String[] cityArray = line.split("\\.");
for (String city : cityArray) {
System.out.println(city);
}
System.out.println();
// 运行结果
北京
上海
广州
深圳
成都
转换为char[]类型数组
使用toCharArray()方法,字符串对象名直接调用返回char[]
String str = "巴山楚水凄凉地";
// 将字符串转换成字符数组
char array[] = str.toCharArray();
System.out.println(array); // 巴山楚水凄凉地
// 将字符数组转换成字符串
String content = new String(array);
System.out.println(content); // 巴山楚水凄凉地