In this article, I will summarize Java 11's String class methods that are useful but unexpectedly unknown, with arbitrary judgment and prejudice. Let's stop and review the classes that we are familiar with, aiming for shorter and better code.
I've come across hundreds of times when I want to remove whitespace before and after a string when writing business code. At that time, the method you often use is the trim method.
" aaa ".trim(); // aaa
However, in the case of the trim method, double-byte spaces are not subject to deletion. The strip method is useful when you want to delete including double-byte spaces. The strip method removes whitespace characters including double-byte spaces before and after.
" aaa ".strip(); // aaa
" aaa ".strip(); // aaa
Furthermore, if you want to delete the character string only at the beginning, use "strip Leading". You can use "strip Trailing" if you want to remove only the trailers.
When writing a test code for checking the number of digits in a character string, do you often write the following source code?
lengthCheck("aaaaa");
If you check about 5 digits, there is no problem, but if it is 120 digits or so, it is troublesome and dirty above all. In such a case, the "repeat" method is useful.
"a".repeat(5); // aaaaa
This is a convenient method that can repeat the specified character string. Since the length of the character string can be managed by the repeat argument, Let's actively use it in the digit check system test code to make it clear and easy to understand.
When creating a business application, I think that it is common to create a CSV file and link it with other systems. The method you want to know in such a case is the "join" method. By using this method, comma-separated strings It will be easy to make.
String.join(",", "a","b","c"); // a,b,c
Of course, the characters stored in the array can also be a character string including the delimiter.
String[] a = {"a","b","c"};
String.join(",", a); // a,b,c
This is the equals method that is commonly used in Java when comparing strings. However, you can also use standard String methods to compare strings.
String t = new String("a");
String s = new String("a");
t == s // false
t.intern() == s.intern() // true
Looking at the JavaDoc, you can see that the case where intern is true is the same as t.equals (s). If you are learning another language and are not familiar with equals, you may want to use it. (I don't see many people using it at work)
In severe situations, OSS cannot be used and StringUtils cannot be used in some cases. In such a case, you should know the isBlank method. You can judge the empty string with the String standard method.
String s = ""; //Empty string
String t = " "; //Half-width space
String r = " "; //Full-width space
String i = null; // null
s.isBlank(); // true
t.isBlank(); // true
r.isBlank(); // ture
i.isBlank(); // true
It should be noted that when it is null, it becomes a nullpointerexception. (Of course) By the way, isEmpty is true only when the length is 0, so the usage is different.
There are many methods that are surprisingly convenient and have a small turn. Once you review JavaDoc, your worries may be resolved. (We will continue to publish it in this article as soon as we find a convenient way to use it.)
Recommended Posts