DEV Community

Cover image for The String Class
Paul Ngugi
Paul Ngugi

Posted on

The String Class

A String object is immutable: Its content cannot be changed once the string is created. You know strings are objects. You can invoke the charAt(index) method to obtain a character at the specified index from a string, the length() method to return the size of a string, the substring method to return a substring in a string, and the indexOf and lastIndexOf methods to return the first or last index of a matching character or a substring. We will take a closer look at strings in this section.

The String class has 13 constructors and more than 40 methods for manipulating strings. Not only is it very useful in programming, but it is also a good example for learning classes and objects.

Constructing a String

You can create a string object from a string literal or from an array of characters. To create a string from a string literal, use the syntax:

String newString = new String(stringLiteral);

The argument stringLiteral is a sequence of characters enclosed inside double quotes. The following statement creates a String object message for the string literal "Welcome to Java":

String message = new String("Welcome to Java");

Java treats a string literal as a String object. Thus, the following statement is valid:

String message = "Welcome to Java";

You can also create a string from an array of characters. For example, the following statements create the string "Good Day":

char[] charArray = {'G', 'o', 'o', 'd', ' ', 'D', 'a', 'y'};
String message = new String(charArray);

A String variable holds a reference to a String object that stores a string value. Strictly speaking, the terms String variable, String object, and string value are different, but most of the time the distinctions between them can be ignored. For simplicity, the term string will often be used to refer to String variable, String object, and string value.

Immutable Strings and Interned Strings

A String object is immutable; its contents cannot be changed. Does the following code change the contents of the string?

String s = "Java";
s = "HTML";

The answer is no. The first statement creates a String object with the content "Java" and assigns its reference to s. The second statement creates a new String object with the content "HTML" and assigns its reference to s. The first String object still exists after the assignment, but it can no longer be accessed, because variable s now points to the new object, as shown in Figure below.

Image description

Because strings are immutable and are ubiquitous in programming, the JVM uses a unique instance for string literals with the same character sequence in order to improve efficiency and save memory. Such an instance is called an interned string. For example, the following statements:

String s1 = "Welcome to Java";
String s2 = new String("Welcome to Java");
String s3 = "Welcome to Java";
System.out.println("s1 == s2 is " + (s1 == s2));
System.out.println("s1 == s3 is " + (s1 == s3));

Image description

display

s1 == s2 is false
s1 == s3 is true

In the preceding statements, s1 and s3 refer to the same interned string—"Welcome to Java"—so s1 == s3 is true. However, s1 == s2 is false, because s1 and s2 are two different string objects, even though they have the same contents.

Replacing and Splitting Strings

The String class provides the methods for replacing and splitting strings, as shown in Figure below.

Image description

Once a string is created, its contents cannot be changed. The methods replace, replaceFirst, and replaceAll return a new string derived from the original string (without changing the original string!). Several versions of the replace methods are provided to replace a character or a substring in the string with a new character or a new substring.
For example,

"Welcome".replace('e', 'A') returns a new string, WAlcomA.
"Welcome".replaceFirst("e", "AB") returns a new string, WABlcome.
"Welcome".replace("e", "AB") returns a new string, WABlcomAB.
"Welcome".replace("el", "AB") returns a new string, WABcome.

The split method can be used to extract tokens from a string with the specified delimiters. For example, the following code

String[] tokens = "Java#HTML#Perl".split("#");
for (int i = 0; i < tokens.length; i++)
System.out.print(tokens[i] + " ");

displays

Java HTML Perl

Matching, Replacing and Splitting by Patterns

Often you will need to write code that validates user input, such as to check whether the input is a number, a string with all lowercase letters, or a Social Security number. How do you write this type of code? A simple and effective way to accomplish this task is to use the regular expression.

A regular expression (abbreviated regex) is a string that describes a pattern for matching a set of strings. You can match, replace, or split a string by specifying a pattern. This is an extremely useful and powerful feature.

Let us begin with the matches method in the String class. At first glance, the matches method is very similar to the equals method. For example, the following two statements both evaluate to true.

"Java".matches("Java");
"Java".equals("Java");

However, the matches method is more powerful. It can match not only a fixed string, but also a set of strings that follow a pattern. For example, the following statements all evaluate to true:

"Java is fun".matches("Java.*")
"Java is cool".matches("Java.*")
"Java is powerful".matches("Java.*")

Java.* in the preceding statements is a regular expression. It describes a string pattern that begins with Java followed by any zero or more characters. Here, the substring matches any zero or more characters.

The following statement evaluates to true.

"440-02-4534".matches("\\d{3}-\\d{2}-\\d{4}")

Here \d represents a single digit, and \d{3} represents three digits.

The replaceAll, replaceFirst, and split methods can be used with a regular expression. For example, the following statement returns a new string that replaces $, +, or # in a+b$#c with the string NNN.

String s = "a+b$#c".replaceAll("[$+#]", "NNN");
System.out.println(s);

Here the regular expression [$+#] specifies a pattern that matches $, +, or #. So, the output is aNNNbNNNNNNc.

The following statement splits the string into an array of strings delimited by punctuation marks.

`String[] tokens = "Java,C?C#,C++".split("[.,:;?]");

for (int i = 0; i < tokens.length; i++)
System.out.println(tokens[i]);`

In this example, the regular expression [.,:;?] specifies a pattern that matches ., ,, :, ;, or ?. Each of these characters is a delimiter for splitting the string. Thus, the string is split into Java, C, C#, and C++, which are stored in array tokens.

Regular expression patterns are complex for beginning students to understand. For this reason, simple patterns are introduced in this section.

Conversion between Strings and Arrays

Strings are not arrays, but a string can be converted into an array, and vice versa. To convert a string into an array of characters, use the toCharArray method. For example, the following statement converts the string Java to an array.

char[] chars = "Java".toCharArray();

Thus, chars[0] is J, chars[1] is a, chars[2] is v, and chars[3] is a.

You can also use the getChars(int srcBegin, int srcEnd, char[] dst,
int dstBegin)
method to copy a substring of the string from index srcBegin to index srcEnd-1 into a character array dst starting from index dstBegin. For example, the following code copies a substring "3720" in "CS3720" from index 2 to index 6-1 into the character array dst starting from index 4.

char[] dst = {'J', 'A', 'V', 'A', '1', '3', '0', '1'};
"CS3720".getChars(2, 6, dst, 4);

Thus, dst becomes {'J', 'A', 'V', 'A', '3', '7', '2', '0'}.

To convert an array of characters into a string, use the String(char[]) constructor or the valueOf(char[]) method. For example, the following statement constructs a string from an array using the String constructor.

String str = new String(new char[]{'J', 'a', 'v', 'a'});

The next statement constructs a string from an array using the valueOf method.

String str = String.valueOf(new char[]{'J', 'a', 'v', 'a'});

Converting Characters and Numeric Values to Strings

Recall that you can use Double.parseDouble(str) or Integer.parseInt(str) to convert a string to a double value or an int value and you can convert a character or a number into a string by using the string concatenating operator. Another way of converting a number into a string is to use the overloaded static valueOf method. This method can also be used to convert a character or an array of characters into a string, as shown in Figure below.

Image description

For example, to convert a double value 5.44 to a string, use String.valueOf(5.44). The return value is a string consisting of the characters '5', '.', '4', and '4'.

Formatting Strings

The String class contains the static format method to return a formatted string. The syntax to invoke this method is:

String.format(format, item1, item2, ..., itemk)

This method is similar to the printf method except that the format method returns a formatted string, whereas the printf method displays a formatted string. For example,

String s = String.format("%7.2f%6d%-4s", 45.556, 14, "AB");
System.out.println(s);

displays

xx45.56xxxx14ABxx

Note that

System.out.printf(format, item1, item2, ..., itemk);

is equivalent to

System.out.print(
String.format(format, item1, item2, ..., itemk));

where the (x) denotes a blank space.

Top comments (0)