In Java, about string types, random verification codes, time types

Source: Internet
Author: User

One, String type: String type

Define a string

String a= "Hello World"; String b= new String ("Hello World");

Concatenation between strings:

String A=ABCD; String B=ABCD; String  c=a+b; System.out.println (c);//The value of the output is "ABCDABCD"      //string type can also be spliced with int type, assuming int d= 555, then     String e=a+d;  then output E, The obtained value is "abcd555";

Some of the common things about strings:

String  sc= "ABCDEFG"; Sc.length ();       Take the length of the string Sc.indexof (c);    Gets the position of the first occurrence of a character, and the returned value is an int of type   C that represents the found character  . It  is important to note that the obtained value requires +1, which is the position of the character, and in the computer, the index of string is starting from 0 sc.lastindexof (c)  //Gets the position of the last occurrence of a character sc.substring (A, B)     / /intercept string, from A to Bsc.trim ()         //Remove only the front and back spaces of the string, without removing the middle sc.replace (      A, B)//Replace All, replaced by a Bsc.replacefirst (A, B)   // The same is replaced, but only once Sc.equals (b)            //string is compared to each other  whether SC equals b  Returns a value of TRUE or Falsesc.split (,)      //splits the string according to certain rules  "," every time a comma is met, a split

  

Get data examples with Sc.indexof ():

String s= "<student><xm> Zhang San </xm><xb> male </xb></student>"; int A=s.indexof ("<xm > ");             Locate the first occurrence of the string and assign the value to aint b=s.indexof ("</xm>"), int c=s.indexof ("<xb>"), int d=s.indexof ("</xb>"); String XM = s.substring (a+4, b);    Because we want to take the value after the character, so we have to skip 4 characters and  +4  intercept from a+4 to b;string XB = s.substring (c+4,d);       The reasons are as above, interception from C+4 to D;  System.out.print ("+xm+", Gender "+XB");      Output

 

Random number or letter or letter + digital mixed Verification Code

String a= new String ("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");   The string yzm= "" is specified first;  for (int i=1;i<=4;i++) {int sj= (int) ((Math.random () *100)%a.length ());       The random number in the For loop, and the length of the string, the random digit is a position in the string c=a.substring (SJ, sj+1);                       Intercepts the character of a position in a string, to the character +1yzm=yzm+c;                            The character stitching}   System.out.print (YZM);                 

  

Second, Time type: Date type

Get Time:

Two methods: 1. Single access, final stitching

Create Calenar Object Calendar a=calendar.getinstance ();//give value int b= a.get (calendar.year) for month and day respectively; int c= a.get (calendar.month) +1 ;     

2, the Calenar format

Create Calenar Object Calendar a= calendar.getinstance ();//Format Time SimpleDateFormat b=new SimpleDateFormat (yyyy-mm-dd hh:mm:ss) ;//to Calender formatted string c= B.format (A.gettime ());//Output System.out.print (c);

  

In Java, about string types, random verification codes, time types

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.