An overview of the

String as opposed to StringBuffer and StringBuilder. The nature of String is immutable. Once created, it is immutable.

Inheritance relationships

Implements three interface implements the Java. IO. Serializable, Comparable, CharSequence

Important member attributes

private final char value[];

The underlying String uses char[] to store data. Reassigning a String indicates that a char[] has been recreated. Char [] does not change, only the String reference has changed.

The constructor

Generates a new String.

public String() {
    this.value = "".value;
}
Copy the code

There are several other overloaded constructors:

public String(java.lang.String)
public String(char[])
public String(char[], int, int)
public String(int[], int, int)
public String(byte[], int, int, int)
public String(byte[], int)
public String(byte[], int, int, java.lang.String)
public String(byte[], int, int, Charset)
public String(byte[], java.lang.String)
public String(byte[], Charset)
public String(byte[], int, int)
public String(byte[])
public String(StringBuffer)
public String(StringBuilder)
Copy the code

The key way to

+ length()

Return string length

public int length() {
    return value.length;
}
Copy the code
+ isEmpty()

Null, returns true if and only if String length is 0

public boolean isEmpty() {
    return value.length == 0;
}
Copy the code
+ charAt(int index)

Returns the character at the specified ordinal number

public char charAt(int index) {
    if ((index < 0) || (index >= value.length)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return value[index];
}
Copy the code
+ getBytes()

Convert strings to byte sequences and store them in a new array using the platform’s default character set.

public byte[] getBytes() {
    return StringCoding.encode(value, 0, value.length);
}
Copy the code
+ equals()

Returns true if the other object is also a String and both strings contain the same sequence of characters.

public boolean equals(Object anObject) { ... }
Copy the code

There is also a public Boolean equalsIgnoreCase() method that ignores case comparisons.

+ contentEquals()

Compared to an object, return true if the character sequence is the same. if

public boolean contentEquals(StringBuffer sb) {
    return contentEquals((CharSequence)sb);
}
public boolean contentEquals(CharSequence cs) {
    ...
}
Copy the code
+ compareTo (String anotherString)

How to compare. Take an example;

  1. String str1 = “abc”; String str2 = “abcd”; Return str1.length – str2.length if the common parts of two strings are exactly the same, but the other string is longer
  2. String a = “123” String b = “134” so at index=1, two different characters in the dictionary, return “2” – “3”, return -1.
public int compareTo(String anotherString) { ... }
Copy the code
+ startsWith()

Determines whether the string begins with the target string.

public boolean startsWith(String prefix) { ... }
public boolean startsWith(String prefix, int toffset) { ... }
Copy the code
+ endsWith()

Determines whether the string ends with the target string.

public boolean endsWith(String suffix) { ... }
Copy the code
+ indexOf()

Evaluates the index value of the position where the target string first occurs.

public int indexOf(int ch, int fromIndex) { ... }
Copy the code
+ lastIndexOf()

Determines the index value of the last occurrence of the target string.

public int lastIndexOf(int ch) { ... }
Copy the code
+ substring(int beginIndex)

Gets a substring of the string based on the start index.

public String substring(int beginIndex) { ... }
Copy the code
+ concat()

Adds the target String to the end of the current String

public String concat(String str) { ... }
Copy the code
+ replace(char oldChar, char newChar)

Replace oldChar in the current String with newChar.

public String replace(char oldChar, char newChar) { ... }
Copy the code
+ matches(String regex)

Determines whether the current string conforms to the target re

public boolean matches(String regex) {
    return Pattern.matches(regex, this);
}
Copy the code
+ contains(CharSequence s)

Determines whether the current String contains the target String sequence.

public boolean contains(CharSequence s) {
    return indexOf(s.toString()) > -1;
}
Copy the code
+ split(String regex)

Splits the current String into several strings, with the target String as the separator, and returns the String array.

public String[] split(String regex) { ... }
public String[] split(String regex, int limit) {... }Copy the code
+ join(CharSequence delimiter, CharSequence… elements)

Concatenates an array of destination strings with the specified delimiter, returning String.

public static String join(CharSequence delimiter, CharSequence... elements) { ... }
public static String join(CharSequence delimiter,
            Iterable<? extends CharSequence> elements) { ... }
Copy the code
+ toLowerCase(Locale locale)

Lower case

public String toLowerCase(Locale locale) { ... }
Copy the code
+ toUpperCase(Locale locale)

Convert to uppercase

public String toUpperCase(Locale locale) { ... }
public String toUpperCase() {... }Copy the code
trim()

Remove sequence space

public String trim() {... }Copy the code
valueOf()

Returns a string implementation representing the target object. A variety of

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}
Copy the code

Realize the principle of

The bottom layer of the String class is implemented using character arrays. All operations are based on character arrays.

Q&A

Problem a

What is the result of executing the following code?

String b = "abc";
String c = "abc";
System.out.println(b.equals(c));

>> true
Copy the code

Because the String “ABC” appears before, when you define strings later, you actually refer directly to the first “ABC” created. So the two objects are the same.

conclusion


You need to distinguish the characteristics and usage scenarios of String, StringBuffer, and StringBuilder. Strings are often used to store sequences of strings that are immutable. If a string needs to change frequently, it is best to use StringBuffer or StringBuilder to store it. The difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe. But it will be relatively slow. StringBuilder is not thread-safe and has high access speed. So strings that change frequently in a single thread should be stored using StringBuilder. To store strings that change frequently in multiple threads, use StringBuffer.

Hope to communicate with you more


After I graduated in 2016, I did front-end work, and now I plan to further study Java development. If you have any questions about the content, please correct them. I also hope that you can give me a thumbs-up and attention, leave me a message, communicate and discuss with me, and make progress together.