一-String类的概述
1.什么是字符串?
字符串是由多个字符组成的一串数据(字符序列)
字符串可以看成是字符数组
2.String类的概述
通过JDK提供的API,查看String类的说明
可以看到这样的两句话。
a:字符串字面值"abc"也可以看成是一个字符串对象。
b:字符串是常量,一旦被创建,就不能被改变。
- 定义字符串的两种方式:
String s = new String("abc");
String str="ccc";
二-String类中的常用方法
1.常见构造方法
- 请看如下例题:
public class MyText {
public static void main(String[] args) {
byte[] bytes={97,98,99,100};
//可以将字节数组,转换成字符串
String s = new String(bytes); // abcd
//可以将字节数组的一部分转成字符串
s=new String(bytes,0,4);
System.out.println(s);
char[] a={'a','b','c','你','好',100};
//可以将字符数组转换成字符串
String s1 = new String(a);
//可以将字符数组的一部分转成字符串
s1=new String(a,3,3); //从3索引处开始,转2个字符
System.out.println(s1);
}
}
运行结果为:
abcd
你好d
2.equals
- 字符串重写了equals方法,比较的是字符串的内容
- 案例演示:
class MyTest {
public static void main(String[] args) {
String s="abc";
String s2="abc";
System.out.println(s.equals(s1));
}
}
运行结果为:
true
- 面试题:区分"=="和"equals()"
观察下案例:
class MyTest {
public static void main(String[] args) {
String s="abc";
String s1 = new String("abc");
String s2="abc";
System.out.println(s==s1);
System.out.println(s==s2);
System.out.println(s.equals(s1));
System.out.println(s);
System.out.println(s1);
System.out.println(s2);
}
}
运行结果为:
false
true
true
abc
abc
abc
分析:
-
双等号比较的是两个对象的地址值是否相同,相同返回true,不同返回 false,
s和s2都没有new一个新的对象,“abc"都存在方法区中,地址值相同,故"s == s1"返回true。而s2是new的一个新的对象,存至对内存中,故"s ==s2” -
而字符串重写了equals方法,比较的是字符串的内容,故三者无论哪两个比较返回到都是true
- 案例演示:
模拟登录, 给三次机会, 并提示还有几次。
public class LoginDemo {
public static void main(String[] args) {
String username = "张三";
String password = "123456";
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 3; i++) {
System.out.println("请输入用户名");
String name = sc.nextLine();
System.out.println("请输入密码");
String pwd = sc.nextLine();
if (username.equals(name) && pwd.equals(password)) {
System.out.println("登录成功");
break;
} else {
if(i==3){
System.out.println("你今天的次数已用");
}else{
System.out.println("用户名或密码输入错误,请重新输入,你还有" + (3 - i) + "次机会");
}
}
}
}
}
分析:
本例题中主要是运用for循环和equals()方法,代码大家自己看看应该都能看懂,这里不做详细介绍
3-toString
- 字符串重写了toString方法,打印的是字符串的内容
- 案例演示:
class MyTest {
public static void main(String[] args) {
String s = new String(); //创建一个空的字符串对象
System.out.println(s.toString());
String s1 = new String("abc");
System.out.println(s1.toString());
}
}
运行结果为:
abc
上题中s为空,所以第一行什么都没有
s1=“abc”,字符串重写了toString方法,s1.toString()打印的是字符串的内容"abc"
4-length
- length可以获取字符串的长度
- 案例演示:
class MyTest {
public static void main(String[] args) {
String s1 = new String("abc");
int length = s1.length(); //获取字符串的长度
System.out.println(length);
}
}
运行结果为:
3
上题中s1的长度为3
3. 案例演示:
把数组中的数据按照指定个格式拼接成一个字符串,举例:
int[] arr = {1,2,3};
拼接结果:
“[1, 2, 3]”
public class MyDemo3 {
public static void main(String[] args) {
String str = "[";
int[] arr = {1, 2, 3};
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
str += arr[i] + "]";
} else {
str += arr[i] + ",";
}
}
System.out.println(str);
}
}
运行结果为
[1,2,3]
5-replace
- replace 字符串的替换
- 案例演示
public class MyText {
public static void main(String[] args) {
String s = "奥巴马是美国总统".replace('马', '牛');
System.out.println(s);
String s1 = "奥巴马是美国总统".replace("奥巴马", "***");
System.out.println(s1);
String s2 = "奥巴马是美国总统特朗普也是美国总总统".replace("奥巴马", "****").replace("特朗普", "***");
System.out.println(s2);
}
}
运行结果为:
奥巴牛是美国总统
***是美国总统
****是美国总统***也是美国总总统
- 案例演示2
去除一个字符串的两端空格和中间空格
public class MyDemo2 {
public static void main(String[] args) {
String s = str.replace(" ", "");
System.out.println(s);
}
}
运行结果为:
asdfdsfasfdsadf
6-trim
- trim 去除字符串两端空格
- 案例演示
class MyText{
public static void main(String[] args) {
String trim = " abc ".trim();
System.out.println(trim);
}
}
运行结果为:
abc
7-compareTo
- 通过字典顺序去比较 返回的值是 ASCII 码的差值 调用者减去传入者
- 案例演示
class MyText{
public static void main(String[] args) {
int i = "abc".compareTo("Abc");
System.out.println(i);
}
}
运行结果为
32
- 案例演示2
若两个字符串长度不同,则比较长度
class MyText{
public static void main(String[] args) {
int i = "abc".compareTo("a");
System.out.println(i);
}
}
运行结果为
2
- 补充:忽略大小写的比较:compareToIgnoreCase
8-toUpperCase和toLowerCase
- 转换大小写
- 案例演示
public class MyTest2 {
public static void main(String[] args) {
String s1 = "abcd".toUpperCase();
System.out.println(s1);
String s2 = "ABCD".toLowerCase();
System.out.println(s2);
}
}
运行结果为:
ABCD
abcd