One, string manipulation
Create a string
String s2 = new String ("Hello World");
String S1 = "Hello World";
1. String connection
When multiple strings are linked, each string is concatenated with +, which is the string link, and a new string is generated after the connection.
2. Get the string length A.lenght ()
Based on the index starting at 0, intercept the string length a.substring (1,3); Intercept to subscript 3 starting from table 1 below.
3. Gets the index position of the specified string IndexOf () method; LastIndexOf () method.
IndexOf (s) return value: The index of the first occurrence of s in the return value string
LastIndexOf (s) return value: Returns the index of the last occurrence of s in the string
4. Remove the space before and after the string trim ()
5. Replace all strings that match the string replace () method
6. Determine if a string is equal equals () method
Comparisons of strings using Equals () are strictly case-sensitive, in which case, if two strings still have the same character and length, true is returned, and false is returned.
7. Judge the beginning of the string StartsWith () method to determine the end of the string EndsWith () method
method is used to determine whether the current string object starts or ends with a character set by a parameter.
8. Case conversion
Converts uppercase letters in a string to a lowercase tolowercase () method;
Converts a lowercase letter in a string to an uppercase toUpperCase () method.
9. String split split (String sign) method
The method completely splits the string according to the delimiter that was developed.
Homework:
Public Static void main1 (string[] args) { = "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" ; for (int i=0;i<4;i++) { int b = (int) (Math.random () *100)% A.length (); = A.substring (b, b+1); System.out.print (a.substring (b, b+1)); } } // randomly generate four-digit verification codes that are not duplicated
Public Static void Main (string[] args) { = "<student><xm> Zhang San </xm><xb> male </xb></student > "; = A.substring (A.indexof ("<xm>") +4,a.indexof ("</xm>")); System.out.println ("Name:" +B); =a.substring (A.indexof ("<xb>") +4,a.indexof ("</xb>")); System.out.println ("Gender:" +C); } // interception of names and gender
Second, date operation: Calendar
(a) Date of reading
Calendar a = Calendar.getinstance ();
D.get (constant);
D.get (calendar.year);//Return to digital year
D.get (calendar.month);//month
D.get (calendar.day_of_month);//day
D.get (Calendar.hour);//
D.get (Calendar.minute);//min
D.get (Calendar.second);//sec
D.get (Calendar.millisecond);//milliseconds
The formatting of the date displays:
Format a display with a date SimpleDateFormat
1. Calendar of dates of creation
Calendar a = Calendar.getinstance ();
2. Create a formatter
SimpleDateFormat f = new SimpleDateFormat ("Formatting style");
YY,YYYY-year
m,mm--Month
D,DD--Days
h,hh--, 12-hour system; 24-hour system when hh--
m,mm-min
s,ss-sec
3. Formatting the Calendar
F.format (date); Note that date is not the calendar;
F.format (A.gettime ()); Use the gettime () function of the Calendar object to convert to a Date object
(b) Date of writing
Calendar a = Calendar.getinstance ();
A.set (year, month, day);
A.set (years, months, days, hours, minutes, seconds);
A.set (constant, value);
C.set (1999,8,12);
C.set (1999, 2,4,18,55,32);
C.set (Calendar.year, 1980);
Public class Dog { publicstaticvoid main (string[] args) { = Calendar.getinstance (); A.set (2002, 2,); New SimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); = B.format (a.gettime ()); System.out.println (c); } // Formatted date display
The current time is not written in seconds
Java string Operations and date operations