In JavaStringis a reference type, which is itself a reference type class. The String class represents character strings. This post will talk about some important features of Java String.

strings represented internally by an array char[]

However, Java compiler Stringhas a special treatment, i.e., it can be directly used "..."as a string:

String s1 = "Hello!";

In fact, strings Stringare char[]represented internally by an array, so the following is also possible:

String s2 = new String(new char[] {'H', 'e', 'l', 'l', 'o', '!'});

Because String used so common, Java supports"..."this type of string literal representation.

Strings are constant

Strings are constant. This immutability is achieved through the internal private final char [] field, and without any method to modify char [].

Let’s see a String example if the String instance had modified?

public class Main {
    public static void main (String args[]) {
        String s = "Hello";
        s= s.toLowerCase();
        s = s + "";

See the result


Why the result looks the string had been modified?

In fact, these operations didn’t modify the string “Hello”. The outcoming is the new String object returned by these operations.

How to compare two string?

Try to run the following code:

public class Main {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "HELLO".toLowerCase();
        System.out.println(s1 == s2);

From the result, you will know that:  two string comparisons must always use equals()methods other than == .

To ignore case comparisons, use the equalsIgnoreCase()method.

The String class also provides multiple methods to search for and extract substrings. Common methods are:

// if contains:
"Hello".contains("ll"); // true

Note that contains()the parameter of the method is CharSequencenot String, for the CharSequence is the parent class of  String.

More examples of searching substrings:

"Hello".indexOf("l"); // 2
"Hello".lastIndexOf("l"); // 3
"Hello".startsWith("He"); // true
"Hello".endsWith("lo"); // true

Example of extracting substrings:

"Hello".substring(2); // "llo"
"Hello".substring(2, 4); "ll"

Note that the index numbers start from 0.

Common methods of Java String

Using isEmpty()andisBlank() to determine if the Java string is empty and blank:

"".isEmpty(); // true
"  ".isEmpty(); // false
"  \n".isBlank(); // true
" Hello ".isBlank(); // false

Use this trim()method to remove blank characters at the beginning and end of the string.  Blank characters, including \tspaces, \r\n, :

"  \tHello\r\n ".trim(); // "Hello"

Note: the content of the string is not changed, but a new string is returned.

Split string

To split a Java string, use a split()method and the parameter is a regular expression as well:

String s = "A,B,C,D";
String[] ss = s.split("\,"); // {"A", "B", "C", "D"}

Stitching strings

Use a static method join() to stitch Java strings, which concatenates an array of strings with the specified string:

String[] arr = {"A", "B", "C"};
String s = String.join("***", arr); // "A***B***C"

Type conversion

To convert any primitive or reference type to a string, you can use static methods valueOf(). This is an overloaded method, and the compiler will automatically select the appropriate method based on the parameters:

String.valueOf(123); // "123"
String.valueOf(45.67); // "45.67"
String.valueOf(true); // "true"
String.valueOf(new Object()); // similar like [email protected]

Converted to char []

String with char[]types can be converted to each other by:

char[] cs = "Hello".toCharArray(); // String -> char[]
String s = new String(cs); // char[] -> String

Read further: