1. Display 4-digit verification Code
Note: Uppercase and lowercase letters, mixed numbers
Public Static void Main (string[] args) { String s= "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz"; Sets the set of verification codes String s1= "" ; for (int i=0;i<4;i++) { int n= (int) (Math.random () *1000)%s.length ( ); // take the remainder as an index to find the verification code S1+=s.substring (n,n+1);//index starts with N and ends at N+1 } System.out.println (S1); }
2. Output Date
Public Static voidMain (string[] args) {//method One://First step: Make a Calendar objectCalendar c=calendar.getinstance (); //The second step of making a formatSimpleDateFormat f=NewSimpleDateFormat ("Yyyy-mm-dd hh-mm-ss");//h for 24-hour, H for 12-hour system//Step three: FormattingString s=F.format (C.gettime ()); System.out.println (s); //Method Two://First step: Make a Calendar objectCalendar c2=calendar.getinstance (); //Step Two: Extract date elements intYear=C2.get (calendar.year); intMonth=C2.get (Calendar.month); intday=C2.get (Calendar.day_of_month); //Step three: Stitching displayString s2=year+ "Year" +month+ "month" +day+ "Day"; SYSTEM.OUT.PRINTLN (S2); }
3. Fetching information from XML
public static void main (string[] args) {
String s =" <student>< Name> Xiao Ming </name><sex> male </sex></student> "; s =s.replaceall ("<student>", ""). ReplaceAll ("</student>", " <student></student> Replace with null characters
int n=s.indexof ("</name>"
String name=s.substring (6,n); // the first 6 characters of "Xiaoming"
int s1=s.indexof ("<sex>") +5; // " male" before "<sex>" index Plus "<sex>" itself length 5 characters
int s2=s.indexof ("</sex>"
String Sex=
SYSTEM.OUT.PRINTLN (name+
}
Java exercises (String Class): Display 4-bit verification code, output date, fetch information from XML