第一章.String
1.String介绍
1.概述:String 类代表字符串
2.特点:
a.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例(对象)实现
凡是带双引号的,都是String的对象
String s = "abc"
"abc"就是对象;String就是对象的数据类型;s就是对象名
b.字符串是常量,它们的值在创建之后不能更改
String s = "hello"
s+="world" -> 会产生新对象
c.String 对象是不可变的,所以可以共享
String s1 = "abc"
String s2 = "abc"
2.String的实现原理
1.jdk8的时候:String底层是一个被final修饰的char数组-> private final char[] value;
2.jdk9开始到之后:底层是一个被final修饰的byte数组-> private final byte[] value;
一个char类型占2个字节
一个byte类型占1个字节 -> 省内存空间
字符串定义完之后,数组就创建好了,被final一修饰,数组的地址值直接定死
3.String的创建
1.String() -> 利用String的无参构造创建String对象
2.String(String original) -> 根据字符串创建String对象
3.String(char[] value) -> 根据char数组创建String对象
4.String(byte[] bytes) -> 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String
a.平台:操作系统
b.操作系统默认字符集:GBK
GBK:一个中文占2个字节
UTF-8:一个中文占3个字节
而且,中文对应的字节一般都是负数
代码在idea中写的,idea启动的时候,会自动加一个启动参数,此启动参数为UTF-8
-Dfile.encoding=UTF-8
5.简化形式:
String 变量名 = ""
public class Demo02String {
public static void main(String[] args) {
//1.String() -> 利用String的无参构造创建String对象
String s1 = new String(); //
System.out.println(s1);
//2.String(String original) -> 根据字符串创建String对象
String s2 = new String("abc");
System.out.println(s2);
//3.String(char[] value) -> 根据char数组创建String对象
char[] chars = {'a','b','c'};
String s3 = new String(chars);
System.out.println(s3);
/*
4.String(byte[] bytes) -> 通过使用平台的默认字符集解码指定的 byte 数组,
构造一个新的 String
*/
byte[] bytes1 = {97,98,99};
String s4 = new String(bytes1);
System.out.println(s4);
byte[] bytes2 = {-97,-98,-99};
String s5 = new String(bytes2);
System.out.println(s5);
byte[] bytes3 = {-28,-67,-96};
String s6 = new String(bytes3);
System.out.println(s6);
//5.简化形式
String s7 = "abc";
System.out.println(s7);
}
}
1.String(char[] value, int offset, int count)->将char数组的一部分转成String对象
value:要转String的char数组
offset:从数组的哪个索引开始转
count:转多少个
2.String(byte[] bytes, int offset, int length)->将byte数组的一部分转成String对象
bytes:要转String的byte数组
offset:从数组的哪个索引开始转
length:转多少个
public class Demo03String {
public static void main(String[] args) {
/* 1.String(char[] value, int offset, int count)->将char数组的一部分转成String对象
value:要转String的char数组
offset:从数组的哪个索引开始转
count:转多少个*/
char[] chars = {'a','b','c','d','e','f'};
String s1 = new String(chars,1,3);
System.out.println(s1);
/* 2.String(byte[] bytes, int offset, int length)->将byte数组的一部分转成String对象
bytes:要转String的byte数组
offset:从数组的哪个索引开始转
length:转多少个*/
byte[] bytes = {97,98,99,100,101};
String s2 = new String(bytes,0,2);
System.out.println(s2);
}
}
4.String 面试题
public class Demo04String {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc"); //创建了一个新的空间
System.out.println(s1==s2);//true
System.out.println(s1==s3);//false
System.out.println(s2==s3);//false
}
}
问1:String s = new String("abc")共有几个对象? 2个
一个new本身 一个是"abc"
问2:String s = new String("abc")共创建了几个对象? 1个或者2个
就看abc有没有提前创建出来了
5.字符串常见问题
public class Demo05String {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = "hello"+"world";
String s5 = s1+"world";
String s6 = s1+s2;
System.out.println(s3==s4);//true
System.out.println(s3==s5);//false
System.out.println(s3==s6);//false
}
}
1.字符串拼接,如果等号右边是字符串字面值拼接,不会产生新对象
2.字符串拼接,如果等号右边有变量参数拼接,会产生新字符串对象
第二章.String的方法
1.判断方法
boolean equals(String s) -> 比较字符串内容
boolean equalsIgnoreCase(String s) -> 比较字符串内容,忽略大小写
public class Demo01String {
public static void main(String[] args) {
String s1 = "abc";
String s2 = new String("abc");
String s3 = "Abc";
System.out.println(s1==s2);//比较地址值了
//boolean equals(String s) -> 比较字符串内容
System.out.println(s1.equals(s2));
//boolean equalsIgnoreCase(String s) -> 比较字符串内容,忽略大小写
System.out.println(s1.equalsIgnoreCase(s3));
System.out.println("=========================");
String s4 = "123";
String s5 = "一二三";
System.out.println(s4.equalsIgnoreCase(s5));//false
String s6 = "壹贰叁";
System.out.println(s5.equalsIgnoreCase(s6));//false
}
}
2.练习1
已知用户名和密码,请用程序实现模拟用户登录。总共给三次机会,登录成功与否,给出相应的提示
步骤:
1.先定义两个字符串,表示注册过的用户名和密码
2.创建Scanner对象,键盘录入用户名和密码
3.比较,如果输入的用户名和密码跟已经注册过的用户名和密码内容一样,就登录成功,否则就登录失败
public class Demo02String {
public static void main(String[] args) {
//1.先定义两个字符串,表示注册过的用户名和密码
String username = "root";
String password = "123";
//2.创建Scanner对象,键盘录入用户名和密码
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请您输入用户名:");
String name = sc.next();
System.out.println("请您输入密码:");
String pwd = sc.next();
//3.比较,如果输入的用户名和密码跟已经注册过的用户名和密码内容一样,就登录成功,否则就登录失败
if (name.equals(username) && pwd.equals(password)) {
System.out.println("登录成功");
break;
} else {
if (i == 2) {
System.out.println("账号冻结");
} else {
System.out.println("登录失败!");
}
}
}
}
}
package com.atguigu.b_stringmethod; import java.util.Objects; public class Demo03String { public static void main(String[] args) { //String s = "abc"; String s = null; //method(s); String s1 = null; String s2 = "abc"; method01(s1,s2); } /* 工具类:Objects 方法:判断两个对象是否相等 -> 自带防空指针作用 public static boolean equals(Object a, Object b) { return (a == b) || (a != null && a.equals(b)); } */ private static void method01(String s1, String s2) { if (Objects.equals(s1,s2)){ System.out.println("是abc"); }else{ System.out.println("不是abc"); } } /* 如果传递过来的对象是null,再去点其他方法,就会空指针 解决:不要让一个字符串变量去点,用确定的字符串去点,可以防空 */ private static void method(String s) { /*if (s.equals("abc")){ System.out.println("是abc"); }else{ System.out.println("不是abc"); }*/ if ("abc".equals(s)){ System.out.println("是abc"); }else{ System.out.println("不是abc"); } } }
3.获取功能
int length() -> 获取字符串长度
String concat(String s)-> 字符串拼接,返回新串儿
char charAt(int index) -> 根据索引获取对应的字符
int indexOf(String s) -> 获取指定字符串在大字符串中第一次出现的索引位置
String subString(int beginIndex) -> 截取字符串,从指定索引开始截取到最后,返回新串儿
String subString(int beginIndex,int endIndex) -> 截取字符串,从beginIndex开始到endIndex结束
含头不含尾,返回新串儿
public class Demo04String {
public static void main(String[] args) {
String s1 = "abcdefg";
//int length() -> 获取字符串长度
System.out.println(s1.length());
//String concat(String s)-> 字符串拼接,返回新串儿
System.out.println(s1.concat