java-String string

Contents of the string with immutable
three common ways +1:

  1. Direct creation:
    three common ways:
    on Pulic String () to create a blank string, does not contain any of the contents
    public String (char [] array) based on the contents of an array of characters, to create the corresponding string.
    public String (byte [] array) according to the content byte array, and to create the corresponding character string.
public class StringDemo {
    public static void main(String[] args) {
        // 创建一个空白字符串
        String s = new String();
        System.out.println("第一个字符串:" + s);
        // 创建字符数组字符串
        char[] ch = {'A', 'B', 'C'};
        String s1 = new String(ch);
        System.out.println("第二个字符串:" + s1);
        // 创建字节数组字符串
        byte[] by = {97, 98, 99};
        String s2 = new String(by);
        System.out.println("第三个字符串:" + s2);
        // 直接创建字符串
        String s4 = "你好";
    }
}

String constant pool:

== number is the address comparison value object, if the contents of the string value needs to be compared

instructions CAUTION:

  1. Any object can receive by Object
  2. equals method has symmetry, i.e. a.equals (b) and b.equals (a) the same effect.
  3. If the two sides compare a constant to a variable, the constant string EDITORIAL recommended, because variables may be empty, for the air-conditioning equals method
    will be reported null pointer exception.
    public boolean equalsIgnoreCase (String anotherString) compare this String to another String, ignoring case considerations. If the lengths of the two strings are the same, and in which the respective characters are equal (ignoring case), it is considered that two strings are equal.
public class equalsDemo {
    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = "abc";
        char[] ch = {'a', 'b', 'c'};
        String str3 = new String(ch);
        System.out.println(str1.equals(str2)); //true
        System.out.println(str1.equals(str3)); //true
        System.out.println(str2.equals(str3)); //true
        String str5 = null;
        System.out.println("c".equals(str5)); //推荐写法
        //System.out.println(str5.equals("c")); //不推荐,会报空指针异常 //java.lang.NullPointerException
        // 不区分大小写进行比较
        String str4 = "abc";
        String str6 = "ABC";
        System.out.println(str4.equalsIgnoreCase(str6)); //true
    }
}

String common methods:
the concat (String STR)
the specified string is connected to this end of the string.
charAt (int index)
Returns the char value at the specified index.
indexOf (String str)
Returns the index of the substring in the first occurrence of the string.
length ()
Returns the length of this string.

substring (int beginIndex)
Returns a new string that is a substring of this string.
substring (int beginIndex, int endIndex)
Returns a new string that is a substring of this string.
getBytes ()
using the platform's default character set encoding is byte String this sequence, storing the result into a new byte array.
char [] toCharArray ()
this string into a new character array.
replace (CharSequence target, CharSequence replacement)
specified literal replacement sequence replacement substring of this string literal match all of the target sequence.

Guess you like

Origin www.cnblogs.com/lishi-jie/p/11698681.html