一、API概述
- 什么是API
- API (Application Programming Interface) :应用程序编程接口
- java中的API
- 指的就是 JDK 中提供的各种功能的 Java类,这些类将底层的实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可,我们可以通过帮助文档来学习这些API如何使用。
- 下载API帮助文档
JDK_API_1.6.CHM
下载:这个百度就可以了有很多- 为什么使用1.6版本,因为官方的都是英文的,1.6的汉化是最好的版本
- 如何使用API帮助文档
- 打开帮助文档,找到索引选项卡中的输入框
- 在输入框中输入Random
- 主要查看四个部分:
- 看类在哪个包下,主要用来导包
- 看类的描述,了解API的作用
- 看构造方法
- 看成员方法
- API使用案例:Scanner
import java.util.Scanner;
public class Demo1Scanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
String s = sc.nextLine();
System.out.println(s);
}
}
二、String概述
- String 类在 java.lang 包下,所以使用的时候不需要导包
- String 类代表字符串,Java 程序中的所有字符串文字(例如“abc”)都被实现为此类的实例也就是说,Java 程序中所有的双引号字符串,都是 String 类的对象
- 字符串不可变,它们的值在创建后不能被更改
三、String构造函数
![在这里插入图片描述](https://img-blog.csdnimg.cn/5b8348e3194641fab30474cbdc2332e5.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5peg5LyR5q2i56ym,size_20,color_FFFFFF,t_70,g_se,x_16)
public class Demo2StringConstructor {
public static void main(String[] args) {
String s1 = new String();
System.out.println(s1);
char[] chs = {'a','b','c'};
String s2 = new String(chs);
System.out.println(s2);
String s3 = new String("123");
System.out.println(s3);
}
}
- 创建字符串对象的区别对比
String s3 = new String("123");
:通过new 创建的字符串对象,每一次 new 都会申请一个内存空间,虽然内容相同,但是地址值不同String s = "abc";
以“”方式给出的字符串,只要字符序列相同(顺序和大小写),无论在程序代码中出现几次,JVM 都只会建立一个 String 对象,并在字符串池中维护
- JDK1.7之后,字符串常量池移动到了堆内存中,而不是在方法区中了
- 当使用双引号创建字符串对象的时候,系统会根据该字符串是否在字符串常量池中存在
public class Demo1 {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
String s3 = "abc";
String s4 = new String("abc");
System.out.println(s3 == s4);
}
}
四、字符串拼接
- 拼接字符串变量和字符串常量
- 当字符串之间使用+号串联(拼接)的时候,系统底层会自动创建一个StringBuilder对象,然后再调用其append方法完成拼接;拼接后再调用toString方法转换为String类型
public class Demo1 {
public static void main(String[] args) {
String s1= "abc";
String s2 = "ab";
String s3 = s2 + "c";
System.out.println(s1 == s3);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/5fbedcdb00ac4a8d95f3834ecd89ad5f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5peg5LyR5q2i56ym,size_20,color_FFFFFF,t_70,g_se,x_16)
- 拼接字符串常量
- java存在常量优化机制,在编译的时候,就会将
"a" + "b" + "c"
优化拼接为"abc"
public class Demo1 {
public static void main(String[] args) {
String s1= "abc";
String s2 = "a" + "b" + "c";
System.out.println(s1 == s2);
}
}
五、字符串比较
- 使用==比较
- == 比较基本数据类型:比较的是具体的值
- == 比较引用数据类型:比较的是对象地址值
- equals和equalsIgnoreCase比较
- equals:比较两个字符串内容是否相同、区分大小写
- equalsIgnoreCase:比较两个字符串内容是否相同、不区分大小写
public class Demo1Equals {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "ABC";
String s3 = "abc";
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equalsIgnoreCase(s2));
}
}
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
String username = "admin";
String password = "123456";
Scanner sc = new Scanner(System.in);
for(int i = 1; i <= 3; i++){
System.out.println("请输入用户名:");
String scUsername = sc.nextLine();
System.out.println("请输入密码:");
String scPassword = sc.nextLine();
if(username.equals(scUsername) && password.equals(scPassword)){
System.out.println("登录成功");
break;
}else{
if(i == 3){
System.out.println("您的登录次数已达到今日上限, 请明天再来");
}else{
System.out.println("登录失败,您还剩余" + (3-i) +"次机会");
}
}
}
}
}
六、字符串遍历和字符统计
- charAt方式实现
- public char charAt(int index):返回指定索引处的char值,字符串的索引也是从0开始的
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
String s = sc.nextLine();
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
System.out.println(c);
}
}
}
- toCharArray方式实现(建议使用)
- public char[] toCharArray( ):将当前字符串拆分为字符数组并返回
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
String s = sc.nextLine();
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
}
}
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
String s = sc.nextLine();
int bigCount = 0;
int smallCount = 0;
int numCount = 0;
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if(c >= 'A' && c <= 'Z'){
bigCount++;
}else if(c >= 'a' && c <= 'z'){
smallCount++;
}else if(c >= '0' && c <= '9'){
numCount++;
}
}
System.out.println("大写字母字符:" + bigCount);
System.out.println("小写字母字符:" + smallCount);
System.out.println("数字字母字符:" + numCount);
}
}
七、字符串截取
- 截取字符串API
String substring(int beginIndex)
:从传入的索引位置处,向后截取,一直截取到末尾,得到新的字符串并返回String substring(int beginIndex, int endIndex)
:从beginIndex索引位置开始截取,截取到endIndex索引位置,得到新字符串并返回(包含头,不包含尾)
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入手机号:");
String telString = sc.nextLine();
String start = telString.substring(0,3);
String end = telString.substring(7);
System.out.println(start + "****" + end);
}
}
八、字符串替换
- 字符串替换API
String replace(CharSequence target, CharSequence replacement)
:将当前字符串中的target内容,使用replacement进行替换,返回新的字符串
import java.util.Scanner;
public class Test6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
String s = sc.nextLine();
String result = s.replace("TMD","***");
System.out.println(result);
}
}
九、字符串切割
- 字符串切割API
String[] split(String regex)
:根据传入的字符串作为规则进行切割;将切割后的内容存入字符串数组中,并将字符串数组返回
import com.sjyl.domain.Student;
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生信息:");
String stuInfo = sc.nextLine();
String[] sArr = stuInfo.split(",");
Student stu = new Student(sArr[0],sArr[1]);
System.out.println(stu.getName() + "..." + stu.getAge());
}
}
public class Student {
private String name;
private String age;
public Student() {
}
public Student(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}