java day12 string

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/amy260231120/article/details/82081802

介绍

package com.heima.string;

public class Demo1_String {

    /**
     * a:字符串字面值"abc"也可以看成是一个字符串对象。
     * b:字符串是常量,一旦被赋值,就不能被改变。
     */
    public static void main(String[] args) {
        //Person p = new Person();
        String str = "abc";                 //"abc"可以看成一个字符串对象
        str = "def";                        //当把"def"赋值给str,原来的"abc"就变成了垃圾
        System.out.println(str);            //String类重写了toString方法返回的是该对象本身

    }

}

构造

package com.heima.string;

public class Demo2_StringCon {

    /**
        * public String():空构造
        * public String(byte[] bytes):把字节数组转成字符串
        * public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
        * public String(char[] value):把字符数组转成字符串
        * public String(char[] value,int index,int count):把字符数组的一部分转成字符串
        * public String(String original):把字符串常量值转成字符串
     */
    public static void main(String[] args) {
        String s1 = new String();
        System.out.println(s1);

        byte[] arr1 = {97,98,99};       
        String s2 = new String(arr1);           //解码,将计算机读的懂的转换成我们读的懂
        System.out.println(s2);

        byte[] arr2 = {97,98,99,100,101,102};
        String s3 = new String(arr2,2,3);       //将arr2字节数组从2索引开始转换3个
        System.out.println(s3);

        char[] arr3 = {'a','b','c','d','e'};    //将字符数组转换成字符串
        String s4 = new String(arr3);
        System.out.println(s4);

        String s5 = new String(arr3,1,3);       //将arr3字符数组,从1索引开始转换3个
        System.out.println(s5);

        String s6 = new String("heima");
        System.out.println(s6);
    }

}

面试题

  1. 常量池中没有这个字符串对象,则创建一个,如果有可以直接使用。
  2. 2.
package com.heima.string;

public class Demo3_String {

    /**
     * * 1.判断定义为String类型的s1和s2是否相等
            * String s1 = "abc"; // 在常量池看一看,有就不创建,没有在创建
            * String s2 = "abc"; // 常量池看一看,因为有所以不创建了
            * System.out.println(s1 == s2); // true             
            * System.out.println(s1.equals(s2));  // true   
        * 2.下面这句话在内存中创建了几个对象?
            * String s1 = new String("abc"); // 2个,常量池里的“abc",新创建的字符串对象,是”abc"的副本,在堆里。          
        * 3.判断定义为String类型的s1和s2是否相等
            * String s1 = new String("abc"); // 堆内存对象的地址值       
            * String s2 = "abc"; // 常量池中地址值
            * System.out.println(s1 == s2);  // false       
            * System.out.println(s1.equals(s2));  // true
        * 4.判断定义为String类型的s1和s2是否相等
            * String s1 = "a" + "b" + "c";
            * String s2 = "abc";
            * System.out.println(s1 == s2); ?           
            * System.out.println(s1.equals(s2)); ?  
        * 5.判断定义为String类型的s1和s2是否相等
            * String s1 = "ab";
            * String s2 = "abc";
            * String s3 = s1 + "c"; // +使得会在堆里创建一个StringBur类,在通过toString转化成String,也是在堆里。
            * System.out.println(s3 == s2); // false
            * System.out.println(s3.equals(s2)); ?  // true
             */
    public static void main(String[] args) {
        //demo1();
        //demo2();
        //demo3();
        //demo4();
        String s1 = "ab";
        String s2 = "abc";
        String s3 = s1 + "c";
        System.out.println(s3 == s2);
        System.out.println(s3.equals(s2));      //true
    }

    private static void demo4() {
        //byte b = 3 + 4;                       //在编译时就变成7,把7赋值给b,常量优化机制
        String s1 = "a" + "b" + "c";
        String s2 = "abc";
        System.out.println(s1 == s2);           //true,java中有常量优化机制 
        System.out.println(s1.equals(s2));   // 重写了equals方法后比较里面的值  //true
    }

    private static void demo3() {
        String s1 = new String("abc");          //记录的是堆内存对象的地址值     
        String s2 = "abc";                      //记录的是常量池中的地址值
        System.out.println(s1 == s2);           //false
        System.out.println(s1.equals(s2));      //true
    }

    private static void demo2() {
        //创建几个对象
        //创建两个对象,一个在常量池中,一个在堆内存中
        String s1 = new String("abc");      
        System.out.println(s1);
    }

    private static void demo1() {               //常量池中没有这个字符串对象,就创建一个,如果有直接用即可
        String s1 = "abc";
        String s2 = "abc";
        System.out.println(s1 == s2);           //true  
        System.out.println(s1.equals(s2));      //true
    }

}

判断功能


package com.heima.string;

public class Demo4_StringMethod {

    /**
        * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
        * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
        * boolean contains(String str):判断大字符串中是否包含小字符串
        * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
        * boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
        * boolean isEmpty():判断字符串是否为空。
        * 
        * ""和null的区别
        * ""是字符串常量,同时也是一个String类的对象,既然是对象当然可以调用String类中的方法
        * null是空常量,不能调用任何的方法,否则会出现空指针异常,null常量可以给任意的引用数据类型赋值
     */
    public static void main(String[] args) {
        //demo1();
        //demo2();
        String s1 = "heima";
        String s2 = "";
        String s3 = null;

        System.out.println(s1.isEmpty());
        System.out.println(s2.isEmpty());
        System.out.println(s3.isEmpty());   //java.lang.NullPointerException
    }

    private static void demo2() {
        String s1 = "我爱heima,哈哈";
        String s2 = "heima";
        String s3 = "baima";
        String s4 = "我爱";
        String s5 = "哈哈";

        System.out.println(s1.contains(s2));        //判断是否包含传入的字符串
        System.out.println(s1.contains(s3));

        System.out.println("------------------");
        System.out.println(s1.startsWith(s4));      //判断是否以传入的字符串开头
        System.out.println(s1.startsWith(s5));

        System.out.println("------------------");
        System.out.println(s1.endsWith(s4));        //判断是否以传入的字符串结尾
        System.out.println(s1.endsWith(s5));
    }

    private static void demo1() {
        String s1 = "heima";
        String s2 = "heima";
        String s3 = "HeiMa";

        System.out.println(s1.equals(s2));      //true
        System.out.println(s2.equals(s3));      //false

        System.out.println("---------------");

        System.out.println(s1.equalsIgnoreCase(s2));    
        System.out.println(s1.equalsIgnoreCase(s3));    //不区分大小写
    }

}

常见方法

    /**
     * * int length():获取字符串的长度。
    * char charAt(int index):获取指定索引位置的字符
    * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
    * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
    * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
    * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
    * lastIndexOf
    * String substring(int start):从指定位置开始截取字符串,默认到末尾。
    * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
     */

String类转换功能

    /**
     * * byte[] getBytes():把字符串转换为字节数组。(通过gbk码表,讲字符串转换成字节数组)
        * char[] toCharArray():把字符串转换为字符数组。
        * static String valueOf(char[] chs):把字符数组转成字符串。
        * static String valueOf(int i):把int类型的数据转成字符串。
            * 注意:String类的valueOf方法可以把任意类型的数据转成字符串。


        * String toLowerCase():把字符串转成小写。(了解)
        * String toUpperCase():把字符串转成大写。
        * String concat(String str):把字符串拼接。
     */

/**
     * * A:String的替换功能及案例演示
            * String replace(char old,char new)
            * String replace(String old,String new)
        * B:String的去除字符串两空格及案例演示
            * String trim()
        * C:String的按字典顺序比较两个字符串及案例演示
            * int compareTo(String str)(暂时不用掌握)
            * int compareToIgnoreCase(String str)(了解)


便利字符串

/**
     * * A:案例演示
     * 需求:遍历字符串
     */
    public static void main(String[] args) {
        String s = "heima";

        for(int i = 0; i < s.length(); i++) {           //通过for循环获取到字符串中每个字符的索引
            /*char c = s.charAt(i);
            System.out.println(c);*/
            System.out.println(s.charAt(i));            //通过索引获取每一个字符   
        }
    }

把一个字符串的首字母转成大写,其余为小写。

package com.heima.test;

public class Test4 {

    /**
     * * A:案例演示
     * 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
     * 链式编程:只要保证每次调用完方法返回的是对象,就可以继续调用
     */
    public static void main(String[] args) {
        String s = "woaiHEImaniaima";
        String s2 = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());
        System.out.println(s2);
    }

}
package com.heima.test;

public class Test5 {

    /**
     * * A:案例演示
        * 需求:把数组中的数据按照指定个格式拼接成一个字符串
            * 举例:
                * int[] arr = {1,2,3};  
            * 输出结果:
                * "[1, 2, 3]"
                * 
        分析:
        1,需要定义一个字符串"["
        2,遍历数组获取每一个元素
        3,用字符串与数组中的元素进行拼接
     */
    public static void main(String[] args) {
        int[] arr = {1,2,3};
        String s = "[";                         //定义一个字符串用来与数组中元素拼接

        for (int i = 0; i < arr.length; i++) {  //{1,2,3}
            if(i == arr.length - 1) {
                s = s + arr[i] + "]";           //[1, 2, 3]
            }else {
                s = s + arr[i] + ", ";          //[1, 2, 
            }
        }

        System.out.println(s);
    }

}
package com.heima.test;

import java.util.Scanner;

public class Test6 {

    /**
     * * A:案例演示
        * 需求:把字符串反转
            * 举例:键盘录入"abc"      
            * 输出结果:"cba"
        *分析:
        *1,通过键盘录入获取字符串Scanner
        *2,将字符串转换成字符数组
        *3,倒着遍历字符数组,并再次拼接成字符串
        *4,打印 
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);                //创建键盘录入对象
        System.out.println("请输入一个字符串:");
        String line = sc.nextLine();                        //将键盘录入的字符串存储在line中

        char[] arr = line.toCharArray();                    //将字符串转换为字符数组

        String s = "";
        for(int i = arr.length-1; i >= 0; i--) {            //倒着遍历字符数组
            s = s + arr[i];                                 //拼接成字符串
        }

        System.out.println(s);
    }

}

猜你喜欢

转载自blog.csdn.net/amy260231120/article/details/82081802
今日推荐