java基础字符串---02

String

概述

String类在java.lang包下,所以使用的时候不需要导包
String类代表字符串,java程序中的所有字符串文字(例如"abc")都被实现为此类的实例
也就是说,java程序中所有的双引号字符串,都是String类的对象

字符串的特点

1.字符串不可变,他们的值在创建后不能被更改
2.虽然String的值是不可变的,但是他们可以被共享
3.字符串效果上相当于字符数组(char[]),但是底层原理是字节数组(byte[])

String构造方法

方法名                                      说明
public String()                        创建一个空白字符串对象,不含有任何内容
public String(char[] chs)          根据字符数组的内容,来创建字符串对象
public String(byte[] bys)          根据字节数组的内容,来创建字符串对象
String s = "abc"                      直接赋值的方式创建字符串对象,内容就是abc

# 例子
public class StringDemo {
    public static void main(String[] args) {
        // 创建一个空白字符串对象
        String s1 = new String();
        System.out.println("s1:"+ s1);

        // 字符数组
        char[] chs = {'a', 'b', 'c', 'd'};
        String s2 = new String(chs);
        System.out.println("s2" + s2); // "abcd"

        // 字节数组
        byte[] bys = {97, 98, 99};
        String s3 = new String(bys);
        System.out.println("s3"+ s3); // 打印的结果为对应的asci码对应的字母"abc"

        // 直接赋值   推荐使用这个方便
        String s4 = "abc";
        System.out.println("s4" + s4);
    }
}

String对象的特点

1.通过new创建的字符串对象,每一次new都会申请一个内存空间,虽然内容相同,但是地址值不同
char[] chs = {"a", "b", "c"}
String s1 = new String(chs);
String s2 = new String(chs);
上面的代码中,JVM会首先创建一个字符数组,然后每一次new的时候都会有一个新的地址,只不过s1和s2参考的字符串内容是相同的

2.以“”方式给出的字符串,只要字符序列相同(顺序和大小写),无论在程序代码中出现几次,JVM都只会创建一个String对象,并在字符串池中维护
String s3 = "abc";
String s4 = "abc";
在上面的代码中,针对第一行代码,JVM会建立一个String对象放在字符串池中,并给s3参考;
第二行则让s4直接参考字符串池中的String对象,也就是说它们本质上是同一个对象

字符串比较

使用 == 做比较
1.基本类型:比较的是数据值是否相同
2.引用类型:比较的是地址值是否相同
字符串是对象,它比较内容是否相同,是通过一个方法来实现的,这个方法叫:equals()
public boolean equals(Object anObject): 将此字符串与指定对象进行比较,由于我们比较的是字符串对象,所以参数直接传递字符串
System.out.println(s1.equals(s2));

案例登录

import java.util.Scanner;

public class StringTest {
    public static void main(String[] args) {
        //用户名和密码
        String username = "liuwei";
        String password = "123456";
        // 定义一个错误次数
        int count = 3;
        for(int i=0;i<3;i++){
            //键盘录入要登录的用户和密码
            Scanner sc = new Scanner(System.in);
            System.out.println("请输出用户名:");
            String name = sc.nextLine();

            System.out.println("请输入密码:");
            String pwd = sc.nextLine();
            if(name.equals(username) && pwd.equals(password)){
                System.out.println("登录成功");
                break;
            }else {
                if(count >1 ){
                    System.out.println("账号或密码错误,请重新输出");
                    count--;
                    System.out.println("还剩:" + count + "次机会");
                    continue;
                }else {
                    System.out.println("输入超过三次,请稍后再输出");
                    break;
                }


            }
        }
    }
}

遍历字符串案例

public class StringTest02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字符串:");
        String chs = sc.nextLine();
        // 字符串.length()  获取字符的长度
        for(int i=0;i<chs.length();i++){
            // 字符串.charAt(索引下标)  根据索引下标取字符串中对应的值
            System.out.println(chs.charAt(i));
        }

    }
}

统计字符次数

import java.util.Scanner;

public class StingTest03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字符串:");
        String chs = sc.nextLine();
        //要统计三种类型的字符串个数,定义三个变量,初始值为0
        int bigCount = 0;
        int smallCount = 0;
        int numberCount = 0;

        //遍历字符串
        for(int i=0;i<chs.length();i++){
            char ch = chs.charAt(i);
            // 判断字符类型
            if(ch>='A' && ch<='Z'){   # 大写字母范围
                bigCount++;
            }else if(ch>='a' && ch<='z'){  # 小写字母范围
                smallCount++;
            }else if(ch>='0' && ch<='9'){
                numberCount++;
            }
        }
        // 输出三种类型的字符个数
        System.out.println("bigCount:" + bigCount);
        System.out.println("smallCount:" + smallCount);
        System.out.println("numberCount:" + numberCount);
    }
}

字符串反转案例

import java.util.Scanner;

public class StringTest04 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个字符串:");
        String line = sc.nextLine();

        String s = reverse(line);
        System.out.println(s);

    }
    //字符串反转方法
    public static String reverse(String s) {
        String ss = "";
        for(int i=s.length()-1;i>=0;i--){
            ss += s.charAt(i);
        }
        return ss;
    }
}
posted @ 2021-03-29 23:36  我在路上回头看  阅读(51)  评论(0编辑  收藏  举报