java基础------String类

字符串多种方法

public class Test_String {
    public static void main(String[] args) {
        /*
        * 字符串多种方法:
        * */
        //1.传统
         String string=new String("ABC");
        System.out.println("输出字符串"+string);
        int s=string.length();
        System.out.println("输出字符串长度"+s);

         //2.字符数组
        char[]nums={'a','b','c'};
        String string1=new String(nums);
        System.out.println("输出字符串"+string1);
        int s1=string.length();
        System.out.println("输出字符串长度"+s1);

        //3.字符编码
        byte[]b={97,98,99};
        String string2=new String(b );
        System.out.println("输出字符串"+string2);
        int s2=string.length();
        System.out.println("输出字符串长度"+s2);

        //字面式形式
        String string3="abc";
        System.out.println("输出字符串"+string3);
        int s3=string.length();
        System.out.println("输出字符串长度"+s2);

    }
}

字符串判断

public class Test_String判断 {
    public static void main(String[] args) {
        //声明两个字符串
        String str1="abc";
        String str2="abcd";
        //判断两个字符是否相等,使用boolean类型判断是 true false
        boolean f=str1.equals(str2);
        System.out.println(f);//输出肯定是false

        //声明两个字符串
        String str3="abc";
        String str4="ABC";
        //判断两个字符是否相等,使用boolean类型判断是 true false
        boolean f1=str3.equalsIgnoreCase(str4);//该方法忽略大小写
        System.out.println(f1);//输出肯定是true


    }
}

字符串获取

public class Test_string获取 {
    public static void main(String[] args) {
        //声明两个字符串(字面值)
        String str1="hello";
        String str2="world";
        //将指定得字符串str2拼接该字符串str1末尾
        String S=str1.concat(str2);
        System.out.println(S);//输出hello world

        //声明一个字符串(字面值)
        String str3="hello";
        /*
        * h e l l o
        * 0 1 2 3 4
        * */
        //返回指定得char 索引值的元素
       char c=str3.charAt(4);
         //给定索引值不能超过字符本身得索引值,这里假如给定索引5得话会报
        // char c=str3.charAt(5);
        // Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
        System.out.println(c);//输出o

        //声明一个字符串(字面值)
        String str4="hello";
        //获取str4中字符串在原字符串中首次出现索引值与charAt相反
        int  S1 =str4.indexOf('l');
        System.out.println(S1);//输出2

        //声明一个字符串(字面值)
        String str5="hello";
        //获取从指定索引值开始一直到末尾的字符
        String  S2 =str5.substring(0);
        System.out.println(S2);//输出hello
        String  S3 =str5.substring(1);
        System.out.println(S3);//输出ello
        //获取从指定索引值开始一直到指定索引值的字符
        String  S4 =str5.substring(0,5);//包含前面,不包含后面
        System.out.println(S4);//输出h
    }
}

字符串转换

public class Test_String转换 {
    public static void main(String[] args) {
        //声明字符串
        String string="abc";
        //将字符串转换成字符元素
        char[] c =string.toCharArray();//字符串是由多个元数构成,转换后是一堆元素而能装一堆元素的就是数组
        //遍历数组
        for(int i=0;i<c.length;i++){
            System.out.print(c[i]);//abc
        }
        System.out.println();

        //声明字符串
        String string1="abc";
        //将String编码转换成新的字节数组
        byte[] c1 =string1.getBytes();//a b c 变成 97 98 99
        //遍历数组
        for(int i=0;i<c1.length;i++){
            System.out.print(c1[i]);//979899
        }
        System.out.println();

        //声明字符串
        String string2="abc";
        //替换字符串中的元素
        String s=string2.replace('b','B');//针对字符
        System.out.println(s);
        String s1=string2.replaceAll("bc","qwe");//针对字符串
        System.out.println(s1);
        String s2=string2.replaceFirst("a","b");//替换字符串中首次出现的
        System.out.println(s2);


    }
}

字符串分割

在这里插入代码片public class Test_String分割 {
    public static void main(String[] args) {
        String string="a b c";
        //分割顾名思义就是切分
        String[]s=string.split(" ");//分割也可以是- ,|.....
        for(int i =0;i<s.length;i++){
            System.out.println(s[i]);
        }
    }
}

字符串案例

import java.util.Scanner;

public class String_Test {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        System.out.println("输入字符串");
        String s=scanner.nextLine();
        int samllcount=0;
        int bigcount=0;
        int numcount=0;
        for (int i=0;i<s.length();i++){
            char c=s.charAt(i);
            if(c>='a'&&c<='z'){
                samllcount++;

            }else if(c>='A'&&c<='Z'){
                bigcount++;

            }else if (c>='0' &&c<='9'){
                numcount++;

            }else{
                System.out.println("非法字符");
            }
        }
        System.out.println("小写"+samllcount);
        System.out.println("大写"+bigcount);
        System.out.println("数字"+numcount);

    }
}

发布了62 篇原创文章 · 获赞 32 · 访问量 2722

猜你喜欢

转载自blog.csdn.net/weixin_45627031/article/details/105397220
今日推荐