"What do you think, Amigo? What is the most popular Java class after int?"
"You already gave me a spoiler in the title of the lesson, Ellie. It's String
!"
"Indeed, it is a spoiler. String
is used absolutely everywhere. It has a bunch of useful methods that you are better off knowing.
"The String
class is the only class other than primitive types whose literals can be used in a switch
statement; the compiler handles string addition and String objects in a special way; String
objects are storied in memory in a special way. Basically, the String
class is a very special class.
"Also, the String
class has a bunch of helper classes whose purpose is to further simplify working with strings in Java. When you learn all this, many thing will really become much easier for you to do."
"I can't wait."
"Well, we'll start from the very core of this ecosystem — the organization of the String
class. The structure of the String
class is actually very simple: inside it is a character array (char
array) that stores all the characters of the string. For example, this is how the word 'Hello' is stored:
It's important.
Actually, this is not quite accurate. Because the String
class is very important, it uses a lot of optimizations, and the data is internally stored not as a character array, but simply as a byte array.
Methods of the String class
The String
class has a lot of methods: it has 18 constructors alone! So, below I will list only the most frequently used ones:
Methods | Description |
---|---|
|
Returns the number of characters in the string |
|
Checks whether the string is an empty string |
|
Checks that the string contains only whitespace characters: space, tab, new line, etc. |
|
Returns the character at the index position in the string. |
|
Returns an array of the characters (a copy) that make up the string |
|
Converts a string to a set of bytes and returns the array of bytes. |
|
Splits a string into multiple substrings. |
|
Joins multiple substrings together |
|
Puts a string into the string pool. |
"That looks very good!"
"Let's write a program that converts a file path from Unix style to Windows style. Unix uses the /
character to separate folders, while Windows uses the \
character.
Solution 1. Using a char
array
Code | Notes |
---|---|
|
Create a Scanner object Read a line from the console Convert a string to a character array Loop over the characters If the character is / ,replace it with \ . Don't forget about escaping.Create a new string based on the character array. Display the string. |
Solution 2. Using the split()
and join()
methods:
Code | Notes |
---|---|
|
Create a Scanner object Read a line from the console Convert string to an array of strings. The / character is used as a separator (the extra two slashes are the result of double escaping).Concatenate all the strings in the array of strings. The \ is used as a separator (we see it escaped).Display the string. |
Solution 3. Using the replace(char oldChar, char newChar)
method:
Code | Notes |
---|---|
|
Create a Scanner object Read a line from the console Simply replace one character with another (the second is escaped) Display the string. |
"I liked the third solution the most. But I'll practice all three."
"Well done, Amigo. I see that you're already impatient to put your new knowledge into practice. The lesson is over."
GO TO FULL VERSION