JAVA6
1.String
String字符串
String是一个类
#类中可以有哪些结果? 属性 方法 构造器
String这个类也有属性、方法、构造器
String一旦声明不可改变(了解)
public static void main(String[] args) {
//String既然是一个类,s1就应该叫做String类的对象
//类如果要创建对象,new,s1并没有new,s1也可以叫对象
String s1 = "abcdefg";
String s4 = "abcdefg";
//s2是通过new创建出来的String的对象
String s2 = new String("abcdefg");
String s3 = new String("abcdefg");
//s1指向的abcdefg和s2指向的abcdefg不在同一个区域内
System.out.println(s1 == s2);//false
System.out.println(s1 == s4);//true
System.out.println(s2 == s3);//false
//双等号比较的是虚地址
//虚地址:对象在内存中的储存位置
//=赋值,无论怎么比较,都是true
//new赋值,用双等号比较,false
}
2.String的应用
(1)比较字符串内容
equals方法:
a.需要传参,传String类型的参数
b.有返回值,类型是boolean类型
c.访问权限public
System.out.println("请输入字符串3:");
String s3 = sc.next();
System.out.println("请输入字符串4:");
String s4 = sc.next();
System.out.println(s3.equals(s4));
(2)长度
字符串 字符串拼接
length方法:
a.不需要传参
b.有返回值,整形int
c.访问权限public
String s1 = "abcdefg";
System.out.println(s1.length());
#面试题:
字符串获取长度的方法和数组获取长度的方法有什么区别?
数组的length是属性,字符串的length()是方法
(3)索引
取出指定下标位置的字符
String s1 = "abcdefg";
System.out.println(s1.charAt(0));
(4)判断指定字符串是否存在
String s1 = "abcdefg";
//返回从左到右遇到的第一个匹配的下标
//返回值为字符串在原字符串的下标,如果不存在,返回-1
System.out.println(s1.indexOf("cd"));
//indexOf(String,int)代表从int位置开始查找,包括当前位置
System.out.println(s1.indexOf("ab",0));
//从后往前找
System.out.println(s1.lastIndexOf("a"));
案例 找出字符串中相同的元素
String s5 = "abhbhdbbiab";
char target = 'a';
int b = 0;
for (int i = 0; i < s5.length(); i++) {
if (s5.charAt(i) == target){
b++;
}
}
System.out.println(target+"在字符串"+s5+"中有"+b+"个");
(5)字符串的截取
如果传一个参数,从指定位置开始截取,直到字符串的末尾,包括起始位置的字符,生成一个新的字符串,不会改变原有数据。
如果传两个参数,包含起始位置,不包含终止位置。
String s5 = "abhbhdbbiab";
System.out.println(s5.substring(1));
System.out.println(s5.substring(1, 2));
案例 身份证
import java.util.Scanner;
public class Ch02 {
public static void main(String[] args) {
//身份证
System.out.println("请输入您的身份账号码:");
String id = sc.next();
Ch02 c = new Ch02();
c.id(id);
}
public void id(String id){
System.out.println("您的生日为:"+id.substring(6,10)+"年"+id.substring(10,12)+"月"+id.substring(12,14)+"日");
if(Integer.parseInt(id.substring(16,17)) %2 == 0){
System.out.println("性别为女");
} else {
System.out.println("性别为男");
}
}
}
(6)转大小写
//转大写
System.out.println(s5.toUpperCase());
//转小写
System.out.println(s5.toLowerCase());
(7)判断开头结尾
String s5 = "abhbhdbbiab";
//判断是否以XXXX开头
System.out.println(s5.startsWith("a"));
System.out.println(s5.startsWith("a", 2));//从第几个下标开始
//判断是否以XXXX结尾
System.out.println(s5.endsWith("b"));
(8)忽略大小写比较 验证码
String s6 = "ABHBHDBBIAB";
System.out.println(s5.equalsIgnoreCase(s6));
(9)去掉字符串前面的空格
String s7 = " eeeeeeee";
System.out.println(s7.trim());
(10)根据指定的字符分割
String s8 = "123,456,789,00";
String [] strings = s8.split(",");
for (String string : strings){
System.out.println(string);
}
(11)字符串的替换
String s9 = "hello";
System.out.println(s9.replace('e', 'c'));
System.out.println(s9.replaceAll("l", "i"));
(12)字符串和数组的转换
String s10 = "asdfgh";
char [] chars = s10.toCharArray();//也可以转换成字节型byte
for(char ch : chars){
System.out.println(ch);
}
//字符串和其它类型的转换
int i =1;
String s11 = String.valueOf(i);
System.out.println(s11);
3.包装器
包装器类型(包装类、封装类) | |
byte | Byte |
short | Short |
int | Integer |
long | Long |
floot | Float |
double | Double |
char | Character |
bollean | Bollean |
原理:
装箱:把int类型包装成了包装器Integer类型
基本数据类型转换成对应的包装器类型
拆箱:把Integer类型转换成int类型
包装器类型转换成对应的基本数据类型
今天所学的内容点很多,能够了解,但不能够熟练掌握
天气晴 坚持晨练 心情良好