- Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
- This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
Mandatory string operations
String utility
String definition
In Java, the String class is the most frequently used, and a String is what we call a String. It is the core class of Java, under the java.lang package.
String source code definition:
public final class String implements java.io.Serializable.Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
/ /...
}
Copy the code
From the above source we can know:
- String is a final modified class, meaning it cannot be inherited.
- The String class implements the Serializable, Comparable, and CharSequence interfaces.
- The String class is stored through a char array, and the array is privately final, meaning that it cannot be modified after creation.
This will help us understand the immutability of strings, and help us define and compare strings.
String operations
We classify string operations into the following six categories:
- Basic operation method
- String null, compare
- String interception and splitting
- String lookup and replacement
- Conversion of strings to other types of data
- String concatenation and formatting
We’ll go through each operation in more detail in the next few videos.
Basic string manipulation methods
Creating a string
The most common way to create a new string is:
String s1;
String s2 = null;
// Initialize a string "a"
String s3 = "a";
// Use byte array to create, suitable for many IO stream codec methods
byte[] bytes = {97.98};
String s4 = new String(bytes);
// Create a new object using the toString method
String s5 = xxObject.toString();
Copy the code
Methods not allowed in large factories:
// New the string once more, which wastes performance and memory
String s21 = new String("a");
// The method of hard rotation without reason
String s22 = 1 + "";
Copy the code
Disallowed methods are already required in many Java coding specifications. Especially using empty string method, who look who despise.
Basic String operations
// Get the length
String.length();
// Compare the contents of strings
Sring.equals(Object anObject);
// Get the byte array
Sring.getBytes()
Copy the code
String null method
String native methods
The most common ones are null and empty strings.
Null here means that the String object does not exist. If you operate on it, a null pointer error will occur.
An empty String is a String that exists but does not contain any characters. In common service scenarios, you need to exclude it.
String s1 = null;
System.out.println("s1 == null --> " + (s1 == null));
// If s2! = null, or you could write it this way
System.out.println("s2.isEmpty() --> " + s2.isEmpty());
// The safe way to write this is as follows
System.out.println("s2 == null || s2.isEmpty() --> " + (s2 == null || s2.isEmpty()));
Copy the code
String utility class
Here we recommend org.apache.com mons. Lang3. Class StringUtils tools.
There are two common methods in it, and probably more than half of programmers don’t know the difference.
Stringutils.isempty () and stringutils.isblank ()
- IsEmpty is the safe way to write native methods, which excludes both null and “”.
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
Copy the code
- IsBlank can also exclude null and “”, but this method also excludes whitespace characters.
public static boolean isBlank(CharSequence cs) {
int strLen = length(cs);
if (strLen == 0) {
return true;
} else {
for(int i = 0; i < strLen; ++i) {
if(! Character.isWhitespace(cs.charAt(i))) {// Key code
return false; }}return true; }}Copy the code
What is a whitespace character?
Contains the common Spaces “”, TAB \t, newline \n, carriage return \r, and other whitespace characters that are not common Unicode definitions.
In specific business scenarios, it is necessary to first exclude such whitespace from interfering with business logic processing, such as communication network transmission. Many people like to use it when all Spaces are excluded. Of course, a more appropriate method is string.trim (), which eliminates all whitespace.
Test the demo code
package com.example.javatech.lesson4;
import org.apache.commons.lang3.StringUtils;
/** * The text is written By the library. * *@author Pandas
* @date2021/9/12 * /
public class StringEmptyBlankDemo {
/** * the string is empty. * More than half of programmers don't know which one to use! * *@paramThe args parameter * /
public static void main(String[] args) {
String s1 = null;
String s2 = "";
String s3 =" \t\r\n "; // Which method should be used?
char[] bytes = {97};
System.out.println(new String(bytes));
System.out.println("s1 == null --> " + (s1 == null));
System.out.println("s2.isEmpty() --> " + s2.isEmpty());
System.out.println("s3.isEmpty() --> " + s3.isEmpty());
System.out.println("s2 == null || s2.isEmpty() --> " + (s2 == null || s2.isEmpty()));
System.out.println("= = = = = = = = = = = = = = = = = = = = =");
// StringUtils -> isEmpty, isBlank
System.out.println("StringUtils.isEmpty --> " + StringUtils.isEmpty(s3));
System.out.println("StringUtils.isBlank --> "+ StringUtils.isBlank(s3)); }}Copy the code
conclusion
How should you choose the empty method? See the summary below.
Learn this and no longer be despised by the older employees.
img
Please bookmark the picture below:
String null operation
This is the content of this issue, I hope to help you new to the line.
I’m Pandas. I’m dedicated to sharing Java programming techniques for Pandas.
If you find this post useful, don’t forget to like it and follow it!