API和API帮助文档
API
- 概念:应用程序编程接口
- 理解:API就是别人写好的东西,可以直接使用
- Java API:指的是JDK提供的各种功能的Java类
- 这些类将底层的实现封装了起来,我们只需要学习如何使用这些类即可
API帮助文档
- 帮助我们更好的使用以及查询各种API的一种工具
String概述
String是Java定义好的一个类,使用的时候也不需要导包
java.lang.String类代表字符串,java程序中所有字符串文字都为此类的对象
字符串的内容是不会改变的,它的对象在创建后不能被改变
package com.itheima.test13;
public class Test {
public static void main(String[] args) {
// 直接赋值
String str = "lhj";
System.out.println(str);
// 空参构造
String str1 = new String();
System.out.println(str1);
// 传递一个字符串,根据传递的字符串内容再创建一个新的字符串对象
String str2 = new String("abc");
System.out.println(str2);
// 传递字符数组,根据字符数组的内容创建一个新的字符串对象
char[] c = {'a', 'b', 'c'};
String str3 = new String(c);
System.out.println(str3);
// 传递一个字节数组,根据字符数组的内容创建一个新的字符串对象
// 在网络中传输的数据都是字节形式
// 到ASCII码查询对应的内容
byte[] b = {97, 98, 99};
String str4 = new String(b);
System.out.println(str4);
}
}
直接赋值
- 当使用双引号直接赋值时,系统会检测该字符串在串池中是否存在
- 不存在:创建新的
- 存在:直接复用
采用new方法进行赋值
即使存在相同的字符数组,也不会直接复用,会一直在堆内存中创建一个小空间,浪费内存空间
字符串的比较
- boolean equals(要比较的字符串) —— 完全一样才是true
- boolean equalsIgnoreCase(要比较的字符串) —— 忽略大小写的比较
package com.itheima.test14;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String str1 = "abc";
String str2 = new String("abc");
String str3 = "Abc";
System.out.println(str1 == str2);
Boolean result1 = str1.equals(str2);
System.out.println(result1);
Boolean result2 = str1.equals(str3);
System.out.println(result2);
Boolean result3 = str1.equalsIgnoreCase(str3);
System.out.println(result3);
Scanner sc = new Scanner(System.in);
System.out.println("请输入abc:");
String str4 = sc.next(); // 用键盘输入abc,看看用“==”号进行比较是否为true
System.out.println(str1 == str4);
}
}
运行结果:
- 键盘输入后所创建的字符串是采用new方法的,会有一个新的地址。因此跟直接赋值的字符串的地址不相同
练习
用户登录
package com.itheima.test15;
import java.util.Scanner;
public class Login {
public static void main(String[] args) {
String rightusername = "lhj";
String rightpassword = "123456";
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名:");
String username = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
if (username.equals(rightusername) && password.equals(rightpassword)) {
System.out.println("登陆成功");
break;
} else {
if (i != 2) {
System.out.println("账号密码错误,请重新输入,你还有" + (2-i) + "次机会");
} else {
System.out.println("用户" + username + "已锁定");
}
}
}
}
}
遍历字符串
package com.itheima.test16;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = sc.next();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
System.out.println(c);
}
}
}
统计字符次数
package com.itheima.test16;
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = sc.next();
int smallCount = 0;
int bigCount = 0;
int numberCount = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= 'a' && c<= 'z') {
smallCount++;
} else if (c >= 'A' && c <= 'Z') {
bigCount++;
} else if (c >= '0' && c <= '9') {
numberCount++;
}
}
System.out.println("小写字符:" + smallCount);
System.out.println("大写字符:" + bigCount);
System.out.println("数字字符:" + numberCount);
}
}
拼接字符串
package com.itheima.test16;
public class Test03 {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
String str = intTostring(arr);
System.out.println(str);
}
public static String intTostring(int[] arr) {
if (arr == null) {
return "";
}
if (arr.length == 0) {
return "[]";
}
String star = "[";
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
star = star + arr[i] + "]";
} else {
star = star + arr[i] + " ,";
}
}
return star;
}
}
字符串反转
package com.itheima.test16;
public class Test04 {
public static void main(String[] args) {
String str = "abc";
String str1 = fanString(str);
System.out.println(str1);
}
public static String fanString(String string) {
String result = "";
for (int i = string.length() - 1; i >= 0; i--) {
char c = string.charAt(i);
result = result + c;
}
return result;
}
}
金额转换
package com.itheima.test17;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入金额:");
int money = sc.nextInt();
if (money >= 0 && money <= 9999999) {
String moneyString = chaiMoney(money);
String moneybuLing = buLing(moneyString);
String result = moneyDanwei(moneybuLing);
System.out.println(result);
break;
} else {
System.out.println("金额无效,重新输入");
}
}
}
public static String moneyDanwei(String money) {
String result = "";
String[] str = {"佰", "拾", "万", "仟", "佰", "拾", "元"};
for (int i = 0; i < money.length(); i++) {
char c = money.charAt(i);
result = result + c + str[i];
}
return result;
}
public static String buLing(String money) {
int number = 7 - money.length();
for (int i = 0; i < number; i++) {
money = "零" + money;
}
return money;
}
public static String chaiMoney(int money) {
String moneyStr = "";
for (int i = 0; i < 7; i++) {
if (money == 0) {
break;
} else {
int m = money % 10;
moneyStr = moneyStr + intToString(m);
money = money / 10;
}
}
return moneyStr;
}
public static String intToString(int number) {
String[] str = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
return str[number];
}
}
字符串截取
手机号屏蔽
package com.itheima.test18;
public class Phone {
public static void main(String[] args) {
String phonenumber = "13498765432";
String str1 = phonenumber.substring(0, 3);
String str2 = phonenumber.substring(7);
String str = str1 + "****" + str2;
System.out.println(str);
}
}
身份证信息查看
package com.itheima.test18;
public class People {
public static void main(String[] args) {
String sfz = "458963200309086317";
String year = sfz.substring(6, 10);
String month = sfz.substring(10, 12);
String date = sfz.substring(12, 14);
System.out.println("生日为:" + year + "年" + month + "月" + date + "日");
char c = sfz.charAt(16);
int num = c - 48;
if (num % 2 == 0) {
System.out.println("性别为:女");
} else {
System.out.println("性别为:男");
}
}
}
敏感词替换
package com.itheima.test18;
public class Word {
public static void main(String[] args) {
String talk = "你玩的真好,CNM,以后别玩了,TMD";
String[] arr = {"TMD", "CNM", "SB", "MLGB"};
for (int i = 0; i < arr.length; i++) {
talk = talk.replace(arr[i], "***");
}
System.out.println(talk);
}
}
StringBuilder
StringBuilder概述
- StringBuilder可以看作是一个容器,创建之后里面的内容是可变的
- 作用:提高字符串的操作效率
StringBuilder构造方法
StringBuilder常用方法
package com.itheima.test19;
public class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("abc");
sb.append(123);
System.out.println(sb);
sb.reverse();
System.out.println(sb);
int length = sb.length();
System.out.println(length);
// 将StringBuilder转换成String类型,就可以用String类型的方法
String str = sb.toString();
System.out.println(str.toUpperCase());
}
}
运行结果:
对称字符串
package com.itheima.test19;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = sc.next();
String str1 = new StringBuilder().append(str).reverse().toString();
if (str.equals(str1)) {
System.out.println("是对称字符串");
} else {
System.out.println("不是对称字符串");
}
}
}
拼接字符串
package com.itheima.test19;
public class Test02 {
public static void main(String[] args) {
int[] arr = {1,2,3};
String str = arrTostring(arr);
System.out.println(str);
}
public static String arrTostring(int[] arr) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
sb.append(arr[i]);
} else {
sb.append(arr[i]).append(", ");
}
}
sb.append("]");
return sb.toString();
}
}
StringJoiner
StringJoiner概述
- StringJoiner跟StringBuilder一样,也可以看成是一个容器,创建之后里面的内容是可变的
- 作用:提高字符串饿操作效率。而且代码编写特别简洁
StringJoiner的构造方法
- StringJoiner没有空参构造
StringJoiner的成员方法
package com.itheima.test19;
import java.util.StringJoiner;
public class Test03 {
public static void main(String[] args) {
StringJoiner sj = new StringJoiner(", ", "[", "]");
sj.add("aaa").add("bbb").add("ccc");
int length = sj.length();
System.out.println(sj);
System.out.println(length);
System.out.println(sj.toString());
}
}
总结
扩展底层原理
package com.itheima.test19;
public class Test04 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
System.out.println(sb.capacity());
System.out.println(sb.length());
StringBuilder sb1 = new StringBuilder();
sb1.append("abc");
System.out.println(sb1.capacity());
System.out.println(sb1.length());
StringBuilder sb2 = new StringBuilder();
sb2.append("abcdefghijklmnopqrstuvwxyz");
System.out.println(sb2.capacity());
System.out.println(sb2.length());
StringBuilder sb3 = new StringBuilder();
sb3.append("abcdefghijklmnopqrstuvwxyz0123456789");
System.out.println(sb3.capacity());
System.out.println(sb3.length());
}
}
运行结果:
字符串原理小结
扩展底层原理3:字符串拼接的底层原理
- 如果没有变量参与,都是字符串直接相加,编译之后就是拼接之后的结果,会复用串池中的字符串
- 如果有变量参与,每一行拼接的代码,都会在内存中创建新的字符串,浪费内存
扩展底层原理4:StringBuilder提高效率原理图
- 所有要拼接的内容都会往StringBuilder中放,不会创建很多无用的空间,节约内存
扩展底层原理5:StringBuilder源码分析
- 默认创建一个长度为16的字节数组
- 添加的内容长度小于16,直接存
- 添加的内容大于16会扩容(原来的容量*2+2)
- 如果扩容之后还不够,以实际的长度为准