【Java基础】常用类(3):String

一、创建字符串:

/*
创建字符串的常见3+1方式:
三种构造方法:
    public String()空白字符串,不含任何内容
    public String(char[] array)根据字符数据的内容,来创建对应的字符串
    public String(byte[] array)根据字节数据的内容,来创建对应的字符串

一种直接创建
 */
public class Demo01String {
    public static void main(String[] args) {
        //空参构造
        String str1 = new String();
        System.out.println("第一个字符串:" + str1);

        //根据字符数组创建字符串
        char[] charArray = {'A', 'B', 'C'};
        String str2 = new String(charArray);
        System.out.println("第二个字符串是:" + str2);//ABC


        //根据字节数组创建字符串
        byte[] byteArray = {97, 98, 99};
        String str3 = new String(byteArray);
        System.out.println("第三个字符串是:" + str3);//abc

        //直接创建
        String str4 = "第三个字符串";
        System.out.println(str4);
    }
}

二、常量池、常用方法(equals、equalsIgnoreCase)

1、对于引用类型,“ == ”进行的是地址值的比较
2、双引号直接写的字符串在“ 常量池 ”中,new的不在池中
 */
public class Demo01StringEquals {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";

        char[] charArray = {'H', 'e', 'l', 'l', 'o'};
        String str3 = new String(charArray);

        System.out.println(str1 == str2);//true,此时比较的是地址值
        System.out.println(str1 == str3);//false
        System.out.println(str2 == str3);//false

        System.out.println(str1.equals(str2));//true,此时比较的是内容
        System.out.println(str2.equals(str3));//true

        System.out.println("Hello".equals(str1));//true
        //false,equals区分大小写
        System.out.println("hello".equals(str1));//false

        String str4 = "hello";
        //equalsIgnoreCase不区分大小写
        System.out.println(str4.equalsIgnoreCase(str1));                               
    }
}

三、字符串获取方法(获取字符串长度、拼接字符串concat、获取指定索引位置的单个字符charAt、第一次出现的索引位置)

public class Demo02StringGet {
    public static void main(String[] args) {
        //字符串长度:length
        int length = "adgjhsuehgjxnbgkhgli".length();
        System.out.println("字符串的长度是:" + length);//20

        //拼接字符串 concat
        String str1 = "Hello";
        String str2 = "World";
        String str3 = str1.concat(str2);
        System.out.println(str1);//Hello
        System.out.println(str2);//World
        System.out.println(str3);//HelloWorld

        //获取指定索引位置的单个字符:charAt
        char c = "Hello".charAt(2);
        System.out.println("字符串中的2号元素是:" + c);//l

        //查找参数字符串在 原字符串中出现的第一次索引位置:indexof
        //如果不存在,则返回结果为-1
        String str4 = "HelloJava";
        int ch = str4.indexOf('e');
        int index = str4.indexOf("llo");
        int j = str4.indexOf("j");
        System.out.println(ch);//1
        System.out.println(index);//2
        System.out.println(j);//-1
    }
}

四、字符串截取方法:substring

public class Demo03SubString {
    public static void main(String[] args) {
        String str1 = "HelloJava";
        //截取从索引值开始到字符串结束的内容
        String str2 = str1.substring(3);

        System.out.println(str1);//HelloJava,并不会改变
        System.out.println(str2);//loJava

        //截取任意字符串,subString(int begin,int end):截取范围[begin, end)
        String str3 = str1.substring(3,5);
        System.out.println(str3);//lo
    }
}

五、字符串转换方法

public class Demo04StringConvert {
    public static void main(String[] args) {
        //转换为字符数组:toCharArray
        char[] charArray = "Hello".toCharArray();
        System.out.println(charArray[0]);//H

        //转换为字节数组:getBytes
        byte[] byteArray = "abc".getBytes();
        System.out.println(byteArray[1]);//97
        for (int i = 0; i < byteArray.length; i++) {
            System.out.println(byteArray[i]);
        }

        //字符串的内容替换:replace
        String str1 = "会不会玩儿啊,你大爷的,你大爷的!";
        String str2 = str1.replace("你大爷的","****");
        System.out.println(str1);//会不会玩儿啊,你大爷的,你大爷的!
        System.out.println(str2);//会不会玩儿啊,****,****!
    }
}

六、字符串的分割方法:split

public class Demo05StringSplit {
    public static void main(String[] args) {
        String str1 = "aaa,bbb,ccc";
        //切割完之后是一个String数组
        String[] array = str1.split(",");
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println("================");

        //注意:按照"."切割时,需要转义才可以: "\\."
        String str2 = "aaa.bbb.ccc";
        String[] array1 = str2.split(".");//此时切割不会成功
        System.out.println("我要动刀了!");
        for (int i = 0; i < array1.length; i++) {
            System.out.println(array1[i]);//无打印内容
        }

        System.out.println("==================");
        String str3 = "aaa.bbb.ccc";
        String[] array2 = str3.split("\\.");
        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i]);
        }
    }
}

 

练习题:键盘输入一个字符串,并统计其中各种字符出现的次数

  解题思路:将字符串转换为字符数组

import java.util.Scanner;

public class Demo07StringCount {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String str = sc.next();

        int countUp = 0;
        int countLow = 0;
        int countNum = 0;
        int countOther = 0;

        char[] charArray = str.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char ch = charArray[i];
            //char类型的数据在进行运算时,自动提升为int,因此可以使用如下
            if('A' <= ch && ch <= 'Z'){
                countUp++;
            }else if('a' <= ch && ch <= 'z'){
                countLow++;
            }else if('0' <= ch && ch <= '9'){
                countNum++;
            }else{
                countOther++;
            }
        }

        System.out.println("大写字母:" + countUp + "个");
        System.out.println("小写字母:" + countLow + "个");
        System.out.println("数字:" + countNum + "个");
        System.out.println("其他符号:" + countOther + "个");
    }
}
posted @ 2020-06-07 23:17  蹲马路牙子吃炒面  阅读(154)  评论(0编辑  收藏  举报