java-String类

/*
java.lang.String类代表字符串。
API当中说:Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
其实就是说:程序当中所有的双引号字符串,都是String类的对象。(就算没有new,也照样是。)

字符串的特点:
    1. 字符串的内容永不可变。【重点】
    2. 正是因为字符串不可改变,所以字符串是可以共享使用的。
    3. 字符串效果上相当于是char[]字符数组,但是底层原理是byte[]字节数组。

创建字符串的常见3+1种方式。
三种构造方法:
    public String():创建一个空白字符串,不含有任何内容。
    public String(char[] array):根据字符数组的内容,来创建对应的字符串。
    public String(byte[] array):根据字节数组的内容,来创建对应的字符串。
一种直接创建:
    String str = "Hello"; // 右边直接用双引号

注意:直接写上双引号,就是字符串对象。
 */
package day7;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

public class StringClass {
    public static void main(String[] args) {
        String A = new String();//空参构造
        System.out.println(A);

        char[] num = new char[]{'a','b','c'};
        String B = new String(num);//char数组构造
        System.out.println(B);

        byte[] num1 = new byte[]{97,98,99};
        String C = new String(num1);//byte数组构造
        System.out.println(C);

        String D = "a,b,c";//直接用双引号构造的进常量池

        commonMethod();

        String E = "asdfASDF1234你好啊";
        count(E);
    }
    public static void commonMethod(){//几种常用的方法
        System.out.println("----------in commonMethod----------");
        System.out.println("asdfasdf21".length());//获取字符的个数
        System.out.println("asdfasdf21".concat("sadfasdf"));//字符串的拼接
        System.out.println("asdfasdf21".charAt(4));//打印第5个字符
        System.out.println("asdfasdf21".indexOf('f'));//查找f首次出现的位置
        String A = "sadf123";
        String B = A.substring(1,5);//从第二个截取到第六个[)左闭右开
        String C = "abc";
        System.out.println("abc".equals(C));//比较两个字符串是否相等,对于引用数据类型,==只是比较内存地址值是否相等
        System.out.println(C.equals("abc"));//不推荐使用这种写法

        //几种转换的方法
        String D = "sadf123";
        char[] E = D.toCharArray();//将字符串转化为数组
        for (int i = 0; i < E.length; i++) {
            System.out.println(E[i]);
        }

        byte[] F = D.getBytes();//获取底层二级制
        System.out.println(F);

        String G = D.replace('f','e');//用e代替f
        System.out.println(G);

        String H = "abc def ghi";
        String[] stringarray = H.split(" ");//按空格分割
        for (int i = 0; i < stringarray.length; i++) {
            System.out.println(stringarray[i]);
        }

        String J = "a.b.c";
        String[] chararray = J.split("\\.");//按.分割
        for (int i = 0; i < chararray.length; i++) {
            System.out.println(chararray[i]);
        }
    }

    public static void count(String E){//统计一种数据类型出现了多少次
        System.out.println("----------in count----------");
        int countUpper = 0;
        int countlower = 0;
        int countnumber = 0;
        int countother = 0;
        char[] chararray = E.toCharArray();
        for (int i = 0; i < chararray.length; i++) {
            char cn = chararray[i];
            if (cn >= 'a' && cn <= 'z'){
                countlower ++;
            }
        else if(cn >= 'A' && cn <= 'Z'){
                countUpper ++;
            }
        else if(cn >= '0' && cn <= '9'){
                countnumber ++;
            }
        else{
                countother ++;
            }
        }
        System.out.println("大写字母有"+countUpper+"个");
        System.out.println("小写字母有"+countlower+"个");
        System.out.println("数字有"+countnumber+"个");
        System.out.println("其他类型有"+countother+"个");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值