preface
We when doing projects often need to deal with string, judgment, action, so I just Java string some common operations are summarized, and recommendations are useful in those tools, I wheeled, after all, we don’t have to repeat to write, provide development efficiency, to go to the rest of the time about his girlfriend ha ha ha!!!!!!!!!!
Built-in Java operations
format
We know that usually we will do string concatenation printing operation, single or use ➕, so it is very low to display forced grid, using format operation is necessary 😄
The format() method of the String class is used to create a formatted String and concatenate multiple String objects, specifying the String format and parameters to generate the formatted String. Shows different converters to convert different data types to strings
The test case
@Test
public void a(a) {
String str = "";
str = String.format("Hi,%s"."Wang li");
System.out.println(str);
str = String.format("Hi,%s:%s.%s"."Wang Na"."Wang li"."A king");
System.out.println(str);
System.out.printf("The capital case of the letter A is: %c % N".'A');
System.out.printf("3>7: %b %n".3 > 7);
System.out.printf("One half of 100 is: %d %n".100 / 2);
System.out.printf("The hexadecimal number for 100 is: %x %n".100);
System.out.printf("The base 8 of 100 is: %o %n".100);
System.out.printf("The 8.5 discount on a 50 yuan book is: % F yuan % N".50 * 0.85);
System.out.printf("The hexadecimal value of the price above is: %a % N".50 * 0.85);
System.out.printf("The index of the price above says: % E % N".50 * 0.85);
System.out.printf("The index of the price above and the shorter length of the float result are: %g %n".50 * 0.85);
System.out.printf("The discount above is %d%% % N".85);
System.out.printf("The hash code for letter A is: %h %n".'A');
}
Copy the code
Print the result
. ____ _ __ _ _ / \ \ / ___ '_ __ _ _) (_ _ __ __ _ \ \ \ \ (\ ___ () |' _ | '_ | |' _ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.7) 2021-09-10 10:42:07 INFO background - preinit org.. Hibernate validator. Internal. Util. Version HV000001: Hibernate Validator 6.1.7.Final 2021-09-10 10:42:07 INFO Main PropertiesTest Starting PropertiesTest using Java 1.8.0_202 on xiangyongdeMacBook-Pro. Local with PID 46281 (Started by xiangyong in /Users/xiangyong/selfProject/project/kmall/kmall-api) 2021-09-10 10:42:07 INFO main PropertiesTest The following profiles are active: Test, mptest _ _ | _ _ _ | _ the _ _ _ - | | | | \ / | _) (_ | | | _ \ | _) | | _ | _ \ / | 3.4.1 track the 2021-09-10 10:42:13 INFO main PropertiesTest Started PropertiesTest in 6.755 seconds (JVM running for 8.519) The result is: false half of 100 is: 50 hexadecimal number of 100 is: 64 hexadecimal number of 100 is: 144 50 yuan book 8.5 discount is: 42.500000 yuan above the hexadecimal number is: The index of the price above 0x1.54p5 represents: 4.250000e+01 the index of the price above and the length of the float result are shorter: 42.5000 the discount above is 85% the hash code of the letter A is: 41Copy the code
For more formatting see the reference here
equals
== = equals == == == == == == == == == == == == =
-
The first difference is that equals is the method and == is the operator;
-
Equals compares the contents of two strings rather than references
-
== Objects are compared for the same reference address; primitive types are compared for their content
@Test
public void a(a) {
// s1 and s2 are different objects
String s1 = new String("aaa");
String s2 = new String("aaa");
System.out.println(s1 == s2); //false
System.out.println(s1.equals(s2)); //true
// s5 and s6 are basic data types
String s5 = "aaa";
String s6 = "aaa";
System.out.println(s5==s6); //true
System.out.println(s5.equals(s6)); //true
// S3 and S4 are references to the same address
String s3 = new String("aaa");
String s4 = s3;
System.out.println(s3==s4); //true
System.out.println(s3.equals(s4)); //true
}
Copy the code
Notice that s5==s6 is true and s1 == s2 is false, because s5 and s6 are basic data types and we are comparing values, s1 and S2 are objects and we are comparing references, so we are comparing references between two different strings
Here are the basic data types:
There are eight basic data types in Java, namely Boolean, byte, short, char, int, float, long, and double. Basic data types are not objects. They are stored on the stack and destroyed when used. And objects are in the heap. If you must use objects Java provides wrapper classes for each of the basic data types, namely Boolean, Byte, Short, Character, Integer, Float, Long, Double (uppercase), and so on.
Click to enter for detailed and in-depth reference
toString
Returns a String representation of the current String object. It is generally used to print object information quickly and easily. All classes inherit object, and any class can override toString
Utility class
hutool
To borrow an official quote:
Hutool is a small and complete Java tool class library, through the static method encapsulation, reduce the cost of learning related API, improve work efficiency, make Java has a functional language like elegance, let the Java language can also “sweet
Click to enter the official website navigation highly recommended
Maven central repository has been published, how many modules can reference a single module or all of them,
Contains the following components:
Apache Commons
Points of comparison · scattered sheet, should not be a special tool library, but it is still quite good to use the most frequently used framework. There are many useful tool classes are not all listed, only the most basic part of the list, interested partners, you can see the official API for more in-depth study
Apache Commons has many subprojects, some of which are commonly used as follows
For details, please click here to enter