Java API:应用程序编程接口
next()和nextLine():
public class Demo1Scanner {
/*
next():遇到空格,就不在录入数据了
结束标记:空格,tab键
nextLine():可以将数据完整的接收过来
结束标记:回车换行符
当nextInt和nextLine结合使用,会结束nextLine
建议:今后键盘录入数据,如果是字符串和整数一起接收,建议使用nextInt()配合next()方法接收字符
*/
public static void main(String[] args) {
//1.创建scanner对象
Scanner sc = new Scanner(System.in);
//2.调用nextline方法接收字符串
//Ctrl + alt + v;快速生成方法的返回值
String s = sc.nextLine();
System.out.println(s);
}
}
String:
public class Demo1String {
/*
java程序中,所有的双引号字符串,都是string这个类的对象
*/
public static void main(String[] args) {
String s1 = "abc123";
int length = s1.length();
System.out.println(length);
//字符串不可变,他们的值创建后不可改变
s1 = "def";//这一步是让s1这个字符串类型的变量,记录了一个新的对象
System.out.println(s1);
/*
student stu = new Student("张三",23);
stu = new Student("李四",24);
*/
}
}
String常见的构造方法:
方法名 | 说明 |
---|---|
public String() | 创建一个空白字符串对象,不含有任何内容 |
public String(char[] chs) | 根据字符数组的内容,来创建字符串对象 |
– | – |
public String(String original | 根据传入的字符串内容,来创建字符串对象 |
String s = “abc”; | 直接赋值的方式创建字符串对象,内容就是abc |
public class Demo2StringConstructor {
/*
注意:
string这个类比较特殊,打印其对象名的时候,不会出现内存地址
而是还对象所记录的真实内容
面向对象-继承,Object类
*/
public static void main(String[] args) {
//public String():创建一个空白字符串对象,不含有任何内容
String s1 = new String();
System.out.println(s1);
//public string(char[] chs):根据字符数组的内容,来创建字符串对象
char[] chs = {'a','b','c'};
String s2 = new String(chs);
System.out.println(s2);
//public String(String original):根据传入的字符串内容,来创建字符串对象
String s3 = new String("123");
System.out.println(s3);
}
}
创建字符串对象的区别对比:
==号作比较:
比较的是具体的值
引用数据类型:比较地址值
用string创建的对象,如果一样,地址值也会一样
通过new创建的字符串对象,每一次new都会申请一个内存空间,虽然内容相同,但是地址值不同
public class Demo3 {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";//地址相同
System.out.println(s1 == s2);
char[] chs = {'a','b','c'};
String s3 = new String(chs);
String s4 = new String(chs);//地址值不同
System.out.println(s3 == s4);
}
}
第一个true
第二个false
结论:双引号创建的字符串对象,在字符串常量池中存储,通过构造方法创建的字符串对象,在堆内存中存储
String字符串特点:
1.Java程序中所有的双引号字符串,都是String类的对象
2.字符串不可变,他们的值在创建后不能被更改
3.虽然string的值是不可变的,但是他们可以被共享
字符串常量池:当使用双引号创建字符串对象的时候,系统会检查该字符串是否在字符串常量池中存在
存在:不会重新创建,而是直接复用
不存在:创建
3.)当字符串之间使用 + 号串联的时候,系统底层会自动创建一个stringbuilder对象,然后再调用其append方法完成拼接
拼接后,在调用其toString方法转换成为String类型
4.)纯粹的常量相加:
public class Test {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "a" + "b" + "c";
System.out.println(s1 == s2);
}
}
true
最终的地址值一样:Java存在常量优化机制,在编译的时候,会将"a"+“b”+“c"拼接为"abc”
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));//false
System.out.println(s1.equals(s3));//true
//忽略大小写比较
System.out.println(s1.equalsIgnoreCase(s2));//true
}
}
例子:
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
String username = "admin";
String password = "123456";
String code = "abcd";
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入验证码:");
String scCode = sc.nextLine();
if(code.equalsIgnoreCase(scCode)==false){
System.out.println("验证码输入错误!");
}else{
System.out.println("验证码输入正确");
break;
}
}
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)+"次机会");
}
}
}
}
}
public char charAt(int index):返回指定所引出的索引
public int length():返回此字符串长度
public class Test2 {
public static void main(String[] args) {
/*
public char charAt(int index):返回指定所引出的索引
public int length():返回此字符串长度
*/
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
String s =sc.nextLine();
for (int i = 0; i <s.length() ; i++) {
//i代表字符串每一个索引
char c = s.charAt(i);
System.out.println(c);
}
}
}
public class Test3 {
public static void main(String[] args) {
/*
public char[] toCharArray():将当前字符串拆分为字符数组并返回
*/
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]);
}
}
}
substring,string replace(target,replacement)
public class Test5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入电话号");
String phone = sc.nextLine();
//截取字符串前三位,包含头,不包含尾
String s1 = phone.substring(0,3);
//截取字符串后四位
String s2 = phone.substring(7);
System.out.println(s1+"****"+s2);
}
}
public class Test6 {
public static void main(String[] args) {
/*
string replace(target,replacement)
将当前字符串中的target内容,使用replacement进行替换,返回新的字符串
*/
Scanner sc = new Scanner(System.in);
System.out.println("请输入");
String s = sc.nextLine();
String s1 = s.replace("TMD","****");
System.out.println(s1);
}
}
split():
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;
}
}
public class Test7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生信息:");
String s = sc.nextLine();
String[] aArr = s.split(",");
Student stu = new Student(aArr[0],aArr[1]);
System.out.println(stu.getName()+"..."+stu.getAge());
}
}
string方法:
StringBuilder:
StringBuilder是一个可变的字符串类,我们课把它看成是一个容器
作用:提高字符串的操作效率
构造方法:
public StringBuilder():创建一个空白可变字符串对象,不含有任何内容
public StringBuilder(String str):根据字符串的内容,来创建可变字符串对象
append:
public StringBuilder append(任意类型):添加数据,并返回对象本身
public StringBuilder reverse():返回相反的字符序列
public int length():返回长度(字符出现的个数)
public String toString():通过toString()就可以实现吧Stringbuilder转换为String
public class Demo1StringBuilder {
public static void main(String[] args) {
long start = System.currentTimeMillis();
StringBuffer sb = new StringBuffer();
for (int i = 1; i <= 50000; i++) {
sb.append(i);
}
System.out.println(sb);
long end = System.currentTimeMillis();
System.out.println((end - start));
}
public static void method(){
long start = System.currentTimeMillis();//获取1970年1月1日0时0分0秒 到当前时间所用多少毫秒
String s = "";
for (int i = 0; i <=50000 ; i++) {
s += i;
}
System.out.println(s);
long end = System.currentTimeMillis();
System.out.println((end - start));
}
}
public class Demo3StringBuilder {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("红色").append("苹果").append("被").append(100);
System.out.println(sb);
sb.reverse();
System.out.println(sb);
System.out.println(sb.length());
String s = sb.toString();
System.out.println(s);
}
}
StringBuilder和String的区别
string:内容是不可变
StringBuilder:内容不可变
StringBuilder和String的相互转化:
1.StringBuilder转换为String:
public String toString():通过toString()就可以实现吧StringBuilder转换为String
2.String转换为StringBuilder:
public StringBuilder(String s):通过构造方法就可以实现吧String转换为StringBuilder
public class Test8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入对称字符串");
String s = sc.nextLine();
//2.将键盘录入的字符串反转
//将字符串封装为stringbuilder对象,目的是为了调用其反转的方法
StringBuffer sb = new StringBuffer(s);
sb.reverse();
String s1 = sb.toString();
//s:string
//sb:StringBuilder
//3.使用反转后的字符串,和原字符串进行比对
if(s.equals(s1)){
System.out.println("是对称字符串");
}else{
System.out.println("不是对称字符串");
}
}
}
public class Test9 {
public static void main(String[] args) {
int[] arr = {1,2,3};
String a =arrayGet(arr);
System.out.println(a);
}
public static String arrayGet(int[] arr){
StringBuffer s = new StringBuffer("[");
for (int i = 0; i <arr.length ; i++) {
if(i == arr.length-1){
s.append(arr[i]).append("]");
}else{
s.append(arr[i]).append(", ");
}
}
return s.toString();
}
}
{
int[] arr = {1,2,3};
String a =arrayGet(arr);
System.out.println(a);
}
public static String arrayGet(int[] arr){
StringBuffer s = new StringBuffer("[");
for (int i = 0; i <arr.length ; i++) {
if(i == arr.length-1){
s.append(arr[i]).append("]");
}else{
s.append(arr[i]).append(", ");
}
}
return s.toString();
}
}