String concatenation, truncation, search, and date conversion

Source: Internet
Author: User

Import java. util. Date;
Public class JavaString {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
// Char to string
Char a [] = {'G', 'O', 'O', 'D ','.'};
String s = new String (a); // All characters
System. out. println (s );
String ss = new String (a, 2, 2); // some characters
System. out. println (ss );
// Connect multiple strings
String s1 = new String ("Hello ");
String s2 = new String ("World ");
String sss = s1 + "" + s2;
System. out. println (sss );
// Connect other data types, int and float
Int booktime = 4;
Float practice = 2.5f;
System. out. println ("I spent" + booktime + "hours reading books;" + practice + "hours on-board exercises .");
// String str = String. format ("I spent {1} hours reading a book every day; {2} hours on-board exercises.", booktime, practice );
// System. out. println (str );
// Obtain the string length:
String str = "We are studentsa ";
Int size = str. length ();
System. out. println ("str. length () =" + size );
// String SEARCH:
Int size1 = str. indexOf ("");
System. out. println ("string SEARCH indexOf (a):" + size1 );
Int size2 = str. lastIndexOf ("");
System. out. println ("string SEARCH: lastIndexOf (a)" + size2 );
// Search for an empty string:
Int size3 = str. indexOf (""); // int size3 = str. indexOf ("");
System. out. println ("null string location:" + size3 );
// Obtain the sub-string: // beginIndex <= endIndex
String substr = str. substring (3, 4); // String substr = str. substring (3 );
System. out. println ("Get substring:" + substr );
// Remove spaces:
String javaStr = "Java Class ";
System. out. println ("original String length:" + javaStr. length ());
System. out. println ("length of the string after spaces are removed:" + javaStr. trim (). length ());
// String replacement
String address = "address ";
String newstr = address. replace ("a", "");
System. out. println (newstr );
// Judge the start and end of a string:
String num1 = "22045612 ";
String num2 = "21304578 ";
Boolean B = num1.startsWith ("22 ");
Boolean b2 = num1.endsWith ("78 ");
Boolean b3 = num2.startsWith ("22 ");
Boolean b4 = num2.endsWith ("78 ");
System. out. println ("does the string num1 start with '22? "+ B );
System. out. println ("is the string num1 ending with '78? "+ B2 );
System. out. println ("does the string num1 start with '22? "+ B3 );
System. out. println ("is the string num1 ending with '78? "+ B4 );
// Judge whether the string is equal
String tom = new String ("I am ");
String jerry = new String ("I am ");
Boolean bb = (tom = jerry );
Boolean bbb = tom. equals (jerry); // specify signorecase
System. out. println ("tom = jerry:" + bb );
System. out. println ("tom. equals (jerry):" + bbb );
// Case-insensitive conversion:
String mystr = new String ("abc DEF ");
String newstrs = mystr. toLowerCase ();
String newstrs2 = mystr. toUpperCase ();
System. out. println (newstrs );
System. out. println (newstrs2 );
// String segmentation:
String splitStr = new String ("abc, def, ghi, gkl ");
String [] newsplitstr = splitStr. split (",");
For (int I = 0; I <newsplitstr. length; I ++)
{
System. out. println (newsplitstr [I]);
}
String [] newsplitstr2 = splitStr. split (",", 2); // you can specify the number of splits.
For (int I = 0; I <newsplitstr2.length; I ++)
{
System. out. println (newsplitstr2 [I]);
}
// Format the string:
// Format the date
// % Te a day in a month (1-31)
// % Tb month
// Full name of % tB month
// % TA full name of the day of the week
// % Ta day of the week
// % Tc
// % TY 4-digit year (2009)
// % The day of the tj year (001-366)
// % Tm month
// % Td the day of the month (01-31)
// % Ty 2-digit year
Date date = new Date ();
String dates = String. format ("% te", date );
String datedays = String. format ("% tj", date );
System. out. println ("Date:" + dates );
System. out. println ("day of the year:" + datedays );
// Time format:
// % TH 2-digit 24 hours: (00-23)
// % TI 2-digit 12 hours: (01-12)
// % Tk 2-digit 24 hours: (0-23)
// % Tl 2-digit 12 hours: (1-12)
// % TM 2-digit minute (00-59)
// % TS 2-digit second (00-60)
// % TL 8-bit Millisecond (000-999)
// % TN 9-bit Millisecond ()
// % Tp upper afternoon,
// % Tz
// % TZ
// % Ts 1970-01-01 00:00:00 seconds passed so far
// % TQ 00:00:00 milliseconds passed so far
String hour = String. format ("% tH", date );
String minute = String. format ("% tM", date );
String second = String. format ("% tS", date );
String msecond = String. format ("% tQ", date );
System. out. println ("now" + hour + "" + minute + "minute" + second + "second ");
System. out. println ("now in milliseconds:" + msecond );
// Date and Time combination:
// % TF year-month-day
// % TD month/day/year
// % Tc all date and time information
// % Tr minutes and seconds last afternoon
// % TT hour/minute/second
// % TR time
String time = String. format ("% tc", date );
String form = String. format ("% tF", date );
String sfm = String. format ("% tT", date );
System. out. println ("all time information:" + time );
System. out. println ("year-month-day format:" + form );
System. out. println ("hour/minute/Second:" + sfm );
System. out. println ("date. toString ():" + date. toString (); // date. toLocaleString ()
System. out. println ("date. toLocaleString ():" + date. toLocaleString ());
// Use a regular expression
// String builder: StringBuilder
// Builder. append (); insert (int offset arg); delete (int start, int end );
StringBuilder bf = new StringBuilder ("Hello ");
Bf. insert (5, "word ");
System. out. println (bf. toString ());
}
 
}

Author: ERDP Technical Architecture"

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.