1. StringTokenizer
class
And now a few more common scenarios involving working with strings. How do you split a string into several parts? There are several ways to do this.
split()
method
The first way to split a string into multiple parts is to use the split()
method. A regular expression that defines a special delimiting string must be passed as an argument. You will learn what a regular expression is in the Java Multithreading quest.
Example:
Code | Result |
---|---|
|
The result will be an array of three strings: |
Simple, but sometimes this approach is excessive. If there are a lot of delimiters (for example, spaces, newline characters, tabs, periods), then you have to construct a rather complex regular expression. It is difficult to read and therefore difficult to modify.
StringTokenizer
class
Java has a special class whose whole job is to split a string into substrings.
This class doesn't use regular expressions: instead, you simply pass in a string consisting of delimiters. The advantage of this approach is that it does not break the entire string into pieces all at once, but instead moves from beginning to end one step at a time.
The class has a constructor and two important methods. We pass the constructor a string that we split into parts, and a string comprised of a set of delimiting characters.
Methods | Description |
---|---|
|
Returns the next substring |
|
Checks whether there are more substrings. |
This class is somehow reminiscent of the Scanner class, which also has nextLine()
and hasNextLine()
methods.
You can create a StringTokenizer
object with this command:
StringTokenizer name = new StringTokenizer(string, delimiters);
Where string
is the string to be divided into parts. And delimiters
is a string, and each character in it is treated as a delimiter. Example:
Code | Console output |
---|---|
|
|
Note that each character in the string passed as the second string to the StringTokenizer
constructor is considered a separator.
2. String.format()
method and StringFormatter
class
Another interesting method of the String class is format()
.
Let's say you have various variables storing data. How do you display them on the screen in one line? For example, we have some data (left column) and desired output (right column):
Code | Console output |
---|---|
|
|
Your code will probably look something like this:
Program code |
---|
|
Such code is not very readable. And if the variable names were longer, then the code would become even more difficult:
Program code |
---|
|
Not very readable, is it?
But this is a common situation in real-world programs, so I want to tell you about a way to write this code more simply and more concisely.
String.format
The String class has a static format()
method: it lets you specify a pattern for assembling a string with data. The general appearance of the command is as follows:
String name = String.format(pattern, parameters);
Example:
Code | Result |
---|---|
|
|
|
|
|
|
The format()
method's first parameter is a format string that contains all the desired text along with special characters called format specifiers (such as %d
and %s
) in the places where you need to insert data.
The format()
method replaces these %s
and %d
format specifiers with the parameters that follow the format string in the parameter list. If we want to insert a string, then we write %s
. If we want to insert a number, then the format specifier is %d
. Example:
Code | Result |
---|---|
|
s is equal to "a=1, b=4, c=3" |
Here is a short list of format specifiers that can be used inside the format string:
Specifier | Meaning |
---|---|
|
String |
|
interger: byte , short , int , long |
|
real number: float , double |
|
boolean |
|
char |
|
Date |
|
% character |
These specifiers indicate the type of data, but there are also specifiers that indicate the order of the data. To get an argument by its number (the numbering starts from one), you need to write "%1$d
" instead of "%d
". Example:
Code | Result |
---|---|
|
s is equal to "a=13, b=12, c=11" |
%3$d
will get the 3rd argument, %2$d
will get the second argument, and %d
will get the very first argument. The %s
and %d
format specifiers refer to arguments regardless of specifiers like %3$d
or %2$s
3. String Pool
Every string specified in code as a string literal is stored in an area of memory called the StringPool
while the program is running. StringPool
is a special array for storing strings. Its purpose is to optimize string storage:
First, the strings specified in code must be stored somewhere, right? Code consists of commands, but data (especially, large strings) must be stored in memory separately from the code. Only references to string objects appear in code.
Second, all identical string literals must be stored in memory only once. And that's just how it works. When your class code is loaded by the Java machine, all string literals are added to the StringPool
if they are not already there. If they are already there, then we simply use a string reference from the StringPool
.
Accordingly, if you assign the same literal to several String
variables in your code, then these variables will contain the same reference. A literal will be added to the StringPool
only once. In all other cases, the code will get a reference to the string already loaded in the StringPool
.
Here's roughly how it works:
Code | Working with the StringPool |
---|---|
|
|
That is why the a
and b
variables will store the same references.
intern()
method
And the best part is that you can programmatically add any string to the StringPool
. To do this, you just need to call the String
variable's intern()
method.
The intern()
method will add the string to the StringPool
if it is not already there, and will return a reference to the string in the StringPool
.
If two identical strings are added to the StringPool
using the intern()
method, the method returns the same reference. This can be used to compare strings by reference. Example:
Code | Note |
---|---|
|
|
|
|
You're unlikely to use this method often, but people love to ask about it in interviews. So it's better to know about it than to not know.
GO TO FULL VERSION