day12

作者: 775444e2a800 | 来源:发表于2018-05-18 13:19 被阅读5次
package com.heima.test;

import java.util.Scanner;
//01-04 String相关
public class Test12 {
    public static void main(String[] args) {
//        demo01();     scanner获取键盘录入
//        demo02();     byte数组 char数组转字符串
//        demo03();     equals equalsIgnoreCase contains startsWith endsWith isEmpty
//        demo04();     length charAt indexOf substring
//        demo05();     字符串遍历
//        demo06();     valueof getBytes toCharArray
//        demo07();     replace trim
    }

    public static void demo07() {
        String str="abcde";

        //replace 替换字符
        String str2=str.replace("a","A");//Abcde
        System.out.println(str2);

        //replace 替换字符串
        String str3=str.replace("ab","AA");//AAcde
        System.out.println(str3);

        String str4="  aa bb c   ";
        String str5=str4.trim();//aa bb c
        System.out.println(str5);
    }

    public static void demo06() {
        //getBytes 字符串转字节数组
        String str="abc";
        byte[] b=str.getBytes();
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);//97 98 99
        }

        //toCharArray 字符串转字符数组
        String str2="abcef";
        char[] c=str2.toCharArray();
        for (int i = 0; i < c.length; i++) {
            System.out.println(c[i]);//abcdef
        }

        //valueOf 字符数组转字符串
        char[] c2={'z','y','w','a','n'};
        String str3=String.valueOf(c2);
        System.out.println(str3);//zywan

        //valueOf 整数转字符串
        int i=100;
        String str4=String.valueOf(i);
        System.out.println(str4);//100字符串
    }

    public static void demo05() {
        String str="abcde";
        for(int i=0;i<str.length();i++){
            System.out.println(str.charAt(i));//a b c d e
        }
    }

    public static void demo04() {
        String str="abcde";

        //length
        int i=str.length();//5
        System.out.println(i);

        //charAt
        char c=str.charAt(4);//e
        System.out.println(c);

        String str2="wohaiheima";

        //indexOf 字符查找 lastindexOf
        int i2=str2.indexOf("a");//3
        System.out.println(i2);

        //indexOf 字符串查找
        int i3=str2.indexOf("hei");//5
        System.out.println(i3);

        //indexOf 字符串查找,跳过4个字符
        int i4=str2.indexOf('a',4);//9
        System.out.println(i4);

        String str3="wohaiheima";

        //substring
        String str4=str3.substring(5);//heima
        System.out.println(str4);

        //substring 截取指定字符串
        String str5=str3.substring(0,5);//wohai
        System.out.println(str5);
    }

    public static void demo03() {
        String str="abcde";
        String str2="abcdE";
        String str3="abcdef";

        //equals()
        boolean b=str.equals(str2);//false
        System.out.println(b);

        //equalsIgnoreCase
        boolean b2=str.equalsIgnoreCase(str2);//true
        System.out.println(b2);

        String str4="我爱heima啊";

        //contains
        boolean b3=str4.contains(str4);//true
        System.out.println(b3);

        //startsWith
        boolean b4=str4.startsWith("我");//true
        System.out.println(b4);

        //endsWith
        boolean b5=str4.endsWith("啊");//true
        System.out.println(b5);

        String str5="";
        boolean b6=str5.isEmpty();//true
        System.out.println(b6);
    }

    public static void demo02() {
        //字节数组转字符串
        byte[] b={97,98,99};
        String s=new String(b);//abc
        System.out.println(s);

        //字节数组部分转字符串
        byte[] b2={97,98,99,100,101,102};
        String s2=new String(b2,2,3);//cde
        System.out.println(s2);

        //字符数组转字符串
        char[] c={'a','b','c'};
        String s3=new String(c);//abc
        System.out.println(s3);

        //字符数组部分转字符串
        char[] c2={'a','b','c','d','e','f'};
        String s4=new String(c2,2,3);//cde
        System.out.println(s4);

        //字符串转字符串
        String str="abc";
        String s5=new String(str);//abc
        System.out.println(s5);
    }

    public static void demo01() {
        Scanner sc=new Scanner(System.in);
        if(sc.hasNextInt()){
            System.out.println(sc.nextInt());
        }else{
            System.out.println("不是合法的数字");
        }
    }
}

相关文章

网友评论

      本文标题:day12

      本文链接:https://www.haomeiwen.com/subject/zulgdftx.html