Java String class methods that are a little useful to know but don't know

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.

Remove whitespace

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.

Character repetition

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.

Useful when creating CSV

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

For those who don't want to compare strings with equals

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)

Empty string judgment when Apache's StringUtils cannot be used

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.

Summary

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.)

Reference material

String

Recommended Posts

Java String class methods that are a little useful to know but don't know
Various methods of Java String class
How to use class methods [Java]
[Java] How to get to the front of a specific string using the String class
Code to escape a JSON string in Java
How to create a class that inherits class information
How to convert a solidity contract to a Java contract class
Java class methods
String class methods
Features that are likely to enter Java 10 for now
Write a class that can be ordered in Java
[Java] How to cut out a character string character by character
[Java] How to erase a specific character from a character string
How to batch initialize arrays in Java that I didn't know when I was a beginner
A concise summary of Java 8 date / time APIs that are likely to be used frequently
About Java String class
11 items I don't know if it's good to know about JAVA
Convert a Java byte array to a string in hexadecimal notation
[Java] How to convert a character string from String type to byte type
How to store a string from ArrayList to String in Java (Personal)
[Java] How to use substring to cut out a character string
I tried to convert a string to a LocalDate type in Java
[MQTT / Java] Implemented a class that does MQTT Pub / Sub in Java
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
[Java] Code that is hard to notice but terribly slow