String字符串对象创建方法,



Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
翻译成人话:程序当中所有双引号字符串常量,都是String类的对象。(就算没有new,也照样是对象。)

字符串的特点:
1. 【字符串的内容不可改变】。(每当你觉得好像是变了的时候,必然是创建了一个新的字符串。)
2. 正是因为字符串不可改变,所以字符串是可以共享使用的。
3. 字符串就是很多个字符连在一起,效果上看类似于char[],但是底层原理用的是byte[]字节数字进行存储。


常见的3+1中字符串创建方式:

三种构造方法
    public String():无参默认构造方法,产生一个空白字符串。
    public String(char[] array):根据字符数组,创建一个对应的字符串。
    public String(byte[] array):根据字节数组,创建一个对应的字符串。

一种直接定义
    String str = "Hello"
 */
public class Demo01String {

    public static void main(String[] args) {
        // 默认的无参空白字符串
        String str1 = new String();
        System.out.println(str1);

        // 根据字符数组创建字符串
        char[] chars = { 'a', 'b', 'c' };
        String str2 = new String(chars);
        System.out.println(str2); // abc

        // 根据字节数组创建字符串(现阶段只考虑英文情况)
        byte[] bytes = { 65, 98, 99 };
        String str3 = new String(bytes);
        System.out.println(str3); // abc

        // 直接定义创建,没有new,也没有调用构造方法,但是仍然是字符串的对象。
        String str4 = "Hello";
        System.out.println(str4); // Hello
    }
}

        // 直接定义创建,没有new,也没有调用构造方法,但是仍然是字符串的对象。
        String str4 = "Hello";
        System.out.println(str4); // Hello
    }
}



*
==运算符:
对于基本数据类型来说,进行数据值比较。
对于引用数据类型来说,进行【地址值】比较。
 */

public class Demo02StringPool {

    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = "abc";

        byte[] bytes = {97, 98, 99};
        String str3 = new String(bytes); // abc

        System.out.println(str1 == str2); // true
        System.out.println(str1 == str3); // false
        System.out.println(str2 == str3); // false
    }

}

字符串的==运算符,是地址值比较。
如果希望进行内容比较,应该使用方法。

字符串当中与比较相关的方法有:
public boolean equals(Object another):让当前字符串和另一个参数进行比较,返回内容是否相同。
备注:参数Object类型,意思是虽然什么参数都行。但是只有参数是字符串类型的时候,才有可能返回true值。
扩展:equals方法具有对称性,a.equals(b)和b.equals(a)效果一样。
注意:如果是一个变量和一个常量进行比较,那么强烈建议将常量写在前面。

public boolean equalsIgnoreCase(String str):让当前字符串和另一个参数字符串进行比较,忽略大小写。
 */
public class Demo03StringEquals {

    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = "hello";

        boolean same = str1.equals(str2);
        System.out.println(same); // true
        System.out.println(str2.equals(str3)); // false
        System.out.println("===========");

        String str4 = null;
        System.out.println("hello".equals(str4)); // false,推荐这么写
//        System.out.println(str4.equals("hello")); // NullPointerException,有安全隐患
        System.out.println("===========");

        System.out.println(str2.equalsIgnoreCase(str3)); // true
        // 注意!只有英文字母才有大小写之分,其他的没有大小写
        System.out.println("HelloWorld123".equalsIgnoreCase("helloworld123")); // true
        System.out.println("HelloWorld12".equalsIgnoreCase("helloworld123")); // false
        System.out.println("壹元钱".equalsIgnoreCase("一元钱")); // false
    }

}

字符串当中与获取相关的常用方法有:
public int length():获取字符串的长度,也就是其中包含的字符个数。
public String concat(String another):将参数字符串拼到结尾位置,返回新字符串。
public char charAt(int index):返回得到指定索引位置的单个字符。
public int indexOf(String str):在当前字符串当中查找参数字符串第一次出现的索引位置,如果没有返回值-1
 */

public class Demo04StringGet {

    public static void main(String[] args) {
        String str = "qervwefqrwfqrwrcervererwfaew";
        System.out.println(str.length()); // 28
        System.out.println("中国".length()); // 2
        System.out.println("=============");

        String str1 = "abc";
        String str2 = str1.concat("123");
        System.out.println(str1); // abc
        System.out.println(str2); // abc123
        System.out.println("=============");

        char ch = "HelloWorld".charAt(5);
        System.out.println(ch); // W
        System.out.println("=============");

        System.out.println("HelloWorld".indexOf("llo")); // 2
        System.out.println("HelloWorldHello".indexOf("llo")); // 仍然是2,只看第一次
        System.out.println("Hello".indexOf("abc")); // -1
    }

}


}



猜你喜欢

转载自blog.csdn.net/qq_28761767/article/details/80979130