String 字符串
String 是一个类,类中可以有哪些结构?
属性、方法、构造器
String 和类一样,也有属性、方法和构造器
示例分析
public static void main(String[] args) {
    String s1 = "abc";
    String s2 = new String("abc");
    String s3 = new String("abc");
    String s4 = "abc";
    System.out.println(s1 == s2); // false
    System.out.println(s2 == s3); // false
    System.out.println(s1 == s4); // true
}String 是一个类,
s1叫做String类的对象
类如果要创建对象,需要new,但是String s1并没有news1也可以叫做对象创建对象是要调用构造器,s2是通过
new创建出来的String类的对象,一旦声明不可改变
s1指向的abc和s2指向的abc,不在同一区域内
双等号比较的是虚地址
但是s1与s4赋值的是常量,不是new新建的,他们两个就会相等
关于等号
=赋值,无论怎么比较,都是truenew赋值,用双等号比较,就是false如何比较字符串的内容
使用equals()方法,例如s1.equals(s2)
equals()方法的特点
1.需要传参,传String类型的参数
2.有返回值,是boolean类型
3.访问权限 public
字符串的一些方法
获取字符串的长度
string.length();,此方法不需要传参
有返回值,类型为整型,访问权限public
String s1 = "abcdefg";
System.out.println(s1.length()); // 结果为7
数组和字符串length的区别
数组的
length是属性,字符串的length()是方法获取字符串指定位置的字符
charAt(index),index即为下角标
与数组类似,将会直接返回该下标所对应的字符
String s1 = "abcdefg";
System.out.println(s1.charAt(0));判断指定字符串是否存在
indexOf(String)判断指定字符是否存在,返回值为字符串在s1的下标
如果不存在,返回-1,返回从左到右遇到的第一个匹配的下标indexOf(String,int)代表从int位置开始查找,包括int的位置s1.lastIndexOf(String)代表从后往前查找s1.lastIndexOf(String,int)代表从后往前查找,从int位置开始查找
String s1 = "abcdefga";
System.out.println(s1.indexOf("a")); // 0
System.out.println(s1.indexOf("a"),1); // 7
System.out.println(s1.lastIndexOf("a")); // 7
System.out.println(s1.lastIndexOf("a",2)); // 0例题1
统计一个字符串在另一个字符串中出现的次数
a 在 s1 中出现了多少次?
public static void main(String[] args) {
    String a = "a";
    int times=0;
    String s1 = "abcdefgabcdaaa";
    int index = 0;
    while (s1.indexOf(a,index) >= 0) {
        times++;
        index= s1.indexOf(a,index)+1;
    }
    System.out.println("a 在 s1 中一共出现了 "+times+" 次!");
}统计一个字符在字符串中出现的次数
public static void main(String[] args) {
    int times=0;
    char a = 'a';
    String s1 = "abcdefgabcdaaa";
    for (int i = 0; i < s1.length(); i++) {
        if (s1.charAt(i)== a) {
            times++;
        }
    }
    System.out.println("a 在 s1 中一共出现了 "+times+" 次!");
}字符串的截取
str.substring(index)传一个参数,从指定位置开始截取,直到字符串的末,包括起始位置str.substring(1, 4)两个参数代表头部和尾部,包括头部位置,但是不包括尾部位置
String str = "abcdefghijklmn";
System.out.println(str.substring(1));
System.out.println(str.substring(1, 4));例题2
输入一个身份证号,根据身份证号输出以下信息
年龄、生日、性别,利用substring()
public String getBirthday(String idCard) {
    return "生日:"+idCard.substring(10, 12)+"-"+idCard.substring(12, 14);
}
public String getGender(String idCard) {
    int sex = Integer.parseInt(idCard.substring(16,17));
    if (sex%2 != 0) {
        return "男";
    }
    return "女";
}
public int getAge(String idCard) {
    return 2022-Integer.parseInt(idCard.substring(6,10));
}
public static void main(String[] args) {
    Ch06_Demo ch06_demo = new Ch06_Demo();
    Scanner scanner = new Scanner(System.in);
    System.out.println((char)(97));
    String idCard = scanner.next();
    System.out.print("年龄:" + ch06_demo.getAge(idCard)+"  ");
    System.out.print(ch06_demo.getBirthday(idCard));
    String gender = ch06_demo.getGender(idCard);
    System.out.println("  性别:" + gender);
}String 的其他方法
全部大写
str.toUpperCase() 将str内的所有字母大写 例如hello -> HELLO
全部小写
str.toLowerCase() 将str内的所有字母小写 例如HELLO -> hello
首字符判断
str.startsWith("c") 判断c是否在str字符串的首位 返回值为booleanstr.startsWith("c",2) 判断c是否在str字符串的第三位(移2位) 返回值为boolean
末尾字符判断
str.endsWith("G") 判断G是否在str字符串的最后一位 返回值为boolean
不区分大小写比较
str.equalsIgnoreCase(str2) 判断str是否等于str2,不管字母大小写是否匹配
格式化输出
str3.trim() 取消前后空格,例如在输入的时候前后多输入了空格,可以用此方法删掉
分隔符
str4.split(",") 分隔后,将会得到一个数组,可以用数组来存放这个值
String[] arr = str4.split(",");
for (String s : arr) {
    System.out.print(s+" ");
}转换成字符串类型
String.valueOf(i) 将会把i以字符串的形式出现
加法等于拼接
任何数据类型和字符串做加法,结果都是字符串
int i=10;
System.out.println(i + "1");字符串转字符数组
str.toCharArray() 将会得到一个char类型的数组
String str = "abcdefg";
char [] array = str.toCharArray();
for (char c : array) {
    System.out.println(c);
}字符串转字节数组
str.getBytes() 将会得到一个char类型的数组
String str = "abcdefg";
byte [] bytes = str.getBytes();
for (byte b : bytes) {
    System.out.println(b);
}包装器类型
包装类、封装类
| 基本数据类型 | 包装器类型 | 
|---|---|
| byte | Byte | 
| short | Short | 
| int | Integer | 
| long | Long | 
| float | Float | 
| double | Double | 
| char | Character | 
| boolean | Boolean | 
自动装箱:把int类型包装成了包装器Integer类型
基本数据类型转换成对应的包装器类型
自动拆箱:把Integer类型转换成int类型
包装器类型转换成对应的基本数据类型
一些新功能
JDK5 之后的新功能
- 自动装箱和自动拆箱
- 增强for循环 (foreach)
- 枚举
JDK7 之后的新功能
- switch...case 可以用字符串
2 条评论
小小ocean竟然想超航宝 
   
  
 航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超  航宝只能我超
 航宝只能我超 
航宝只能我超
航宝是我的 谁也别想和我抢