Several useful classes in JDK

Source: Internet
Author: User

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.

  1. 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.
  2. 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.
    • Period ". ": No matter where this symbol is used, it can only match one character, that is, it can only represent the position of one character, for example:" B. Y "this mode, it can match strings in the form of: Boy, Bay, bty
    • Square brackets "[]": This symbol can specify a single character of the matched string, for example, B [OIA] y mode, the matching string can only be in the form of boy, biy, and Bay. Other strings are not allowed to appear.
    • Or symbol "|": the basic meaning of this symbol is the OR operation. Its mode allows multiple characters, not only a single character, for example: B (o | I | A | EE |) Y. The matching string can be: Boy, biy, Bay, and beey. It can be seen that the matching can be multiple characters, and parentheses are also used here, which can be used for grouping.
    • Symbol indicating the number of matches
      Symbol Times
      * 0 or multiple times
      + 1 time or multiple times
      ? 0 or 1 time
      {N} Exactly n times
      {N, m} From n times to m times
    • The hyphen "-" indicates a range, for example, from 0 to 9. Therefore, when matching the hyphen in a number, an escape character "/" must be added before it. For example, if the phone number is 010-89201293 and you want to match such a string, you can write the following code: [0-9] {3}/-[0-9] {8 }.
    • No: "^". If it is used in square brackets, "^" indicates a character that you do not want to match. For example, [^ B] indicates that the first character cannot start with B.
    • Space Character "/s", matching all white spaces, including TAB characters
    • Other Symbols
      Symbol Equivalent Regular Expression
      /D [0-9]
      /D [^ 0-9]
      /W A-Z0-9
      /W [^ A-Z0-9]
      /S [/T/N/R/F]
      /S [^/T/N/R/F]

      For example, the phone number is/d {3}/-/d {8}

    • GROUP:
      For example, the following program:
      Pattern P = pattern. Compile ("(// d {3}) //-(// D *)");
      String STR = "234 werweklsf2132wer123-234jkfs010-3232-wer ";
      Matcher M = P. matcher (STR );
      Boolean result = M. Find ();
      System. Out. println ("the number of matched groups obtained by this query is:" + M. groupcount ());
      While (result ){
      System. Out. println ("matched characters:" + M. Group ());
      Result = M. Find ();
      }
      There are two groups, but the matched string must also be (/d {3})/-(/D *) to match the string. The result of this example is:
      The number of matched groups obtained by this query is: 2
      Matched characters: 123-234
      Matched characters: 010-3232
      In this example, It is very convenient to use it to match strings. Another example:
      Pattern P = pattern. Compile ("[, // s] + ");
      String [] result = P. Split ("* 5, 6 25 7-8 */5 ");
      For (INT I = 0; I <result. length; I ++)
      System. Out. println (result [I]);
      This example is used to divide the given string by space. It can be used to divide the string "* 5, 6 25 7-8 */5" into a group of strings. The result is:
      *
      5
      6
      25
      7-8
      */5
      Look, how convenient it is to use it to divide strings by space! Haha.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.