Java string Operations and date operations

Source: Internet
Author: User
Tags string indexof

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

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.