Several useful classes in JDK
In today's work, we have applied the following classes, which are very useful. Here we will briefly explain their usage rules and instructions, just remember it.
- Java. util. stringtokenizer Application
As you may know, it can be used to split strings. Yes, I encountered this situation at work today, so I learned it by the way.
Example:
Stringtokenizer ST = new stringtokenizer ("this is a test ");
While (St. hasmoretokens ()){
System. Out. println (St. nexttoken ());
}
The execution result of this program is to split the string by space. This may be its default splitting method.
If you change "this is a test" to "this, is, A, test", you must modify its character segmentation rules, you need to change it to the following in its constructor:
Stringtokenizer ST = new stringtokenizer ("this, is, A, test ",",");
In this way, the string can be separated by commas. The printed result is the same as the previous one.
If you want to print the comma, you can add true to the constructor.
Stringtokenizer ST = new stringtokenizer ("this, is, A, test", true );
In this way, the comma can be printed. It can be seen that it is very convenient to use it to separate a string. As long as you specify a character to separate the string, you can use this symbol to separate the string. During the split process, you can also view your split statistics. You can use the counttokens () method of the stringtokenizer class to view the statistics in the split process, it can be seen that it is decreasing.
- Application of Java. util. RegEx. matcher and Java. util. RegEx. Pattern
As we all know, this is used to match regular expressions. I will briefly introduce it here:
First, you need to establish a mode and then match the string to be queried based on this mode.
First, we will introduce the basic knowledge of Regular Expressions: Introduction to symbols.