Basic knowledge of strings, regular expressions, date manipulation

1. Common methods of strings

2. Regular expressions

What it can be used for: defines a pattern for strings; can be used to search, edit, or manipulate text; is not limited to one language, but each language has subtle differences.

The java.util.regex package mainly contains the following three classes:

  • Pattern class:

    The pattern object is a compiled representation of a regular expression. The Pattern class has no public constructor. To create a Pattern object, you must first call its public static compile method, which returns a Pattern object. The method accepts a regular expression as its first parameter.

  • Matcher class:

    The Matcher object is the engine that interprets and matches the input string. Like the Pattern class, the Matcher has no public constructor. You need to call the matcher method of the Pattern object to get a Matcher object.

  • PatternSyntaxException:

    PatternSyntaxException is an optional exception class that represents a syntax error in a regular expression pattern.

Example:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class RegexMatches
{
    public static void main( String args[] ){
 
      // find in the string according to the specified pattern
      String line = "This order was placed for QT3000! OK?";
      String pattern = "(\\D*)(\\d+)(.*)";
 
      // Create Pattern object
      Pattern r = Pattern.compile(pattern);
 
      // Now create the matcher object
      Matcher m = r.matcher (line);
      if (m.find( )) {
         System.out.println("Found value: " + m.group(0) );
         System.out.println("Found value: " + m.group(1) );
         System.out.println("Found value: " + m.group(2) );
         System.out.println("Found value: " + m.group(3) );
      } else {
         System.out.println("NO MATCH");
      }
   }
}

operation result:

Found value:This order was placed for QT3000! OK?Found value:This order was placed for QT
Found value:3000Found value:! OK? 
  
 

Regular expression syntax:

3. Date operation

a.Date type, common methods:


b. Date formatting SimpleDateFormat class

Use the SimpleDateFormat class constructor to pass parameters: E yyyy-MM-dd HH/hh:mm:ss:SSS

HH is 24 hours / hh is 12 hours, example:

import java.util. *;
import java.text.*;
 
public class DateDemo {
   public static void main(String args[]) {
 
      Date dNow = new Date( );
      SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
 
      System.out.println("Current Date: " + ft.format(dNow));
   }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325501624&siteId=291194637