Java-java String concatenation, truncation, search, and date conversion

Source: Internet
Author: User

Import Java. util. date; <br/> public class javastring {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> // Char to string <br/> char a [] = {'G', 'O ', 'o', 'D ','. '}; <br/> string S = new string (a); // All characters <br/> system. out. println (s); <br/> string Ss = new string (A, 2, 2); // some characters <br/> system. out. println (SS); <br/> // multiple strings Connection <br/> string S1 = new string ("hello"); <br/> string S2 = new string ("world "); <br/> string SSS = S1 + "" + S2; <br/> system. out. println (SSS); <br/> // connect other data types, int and float <br/> int booktime = 4; <br/> float practice = 2.5f; <br/> system. out. println ("I spent" + booktime + "hours to read books;" + practice + "hours to practice on the computer. "); <br/> // string STR = string. format ("I spent {1} hours reading books every day; {2} hours practicing on the computer. ", booktime, practice); <br/> // system. out. Println (STR); <br/> // obtain the string length: <br/> string STR = "We Are studentsa"; <br/> int size = Str. length (); <br/> system. out. println ("str. length () = "+ size); <br/> // string SEARCH: <br/> int size1 = Str. indexof ("A"); <br/> system. out. println ("string SEARCH indexof (a):" + size1); <br/> int size2 = Str. lastindexof ("A"); <br/> system. out. println ("string SEARCH: lastindexof (a)" + size2); <br/> // search for an empty string: <br/> int size3 = Str. indexof (" "); // Int size3 = Str. indexof (""); <br/> system. out. println ("null string location:" + size3); <br/> // obtain the substring: // beginindex <= endindex <br/> string substr = Str. substring (3, 4); // string substr = Str. substring (3); <br/> system. out. println ("Get substring:" + substr); <br/> // remove space: <br/> string javastr = "Java class"; <br/> system. out. println ("original String Length:" + javastr. length (); <br/> system. out. println ("length of the string after spaces are removed:" + javastr. trim (). Length (); <br/> // string replacement <br/> string address = "Address"; <br/> string newstr = address. replace ("A", "A"); <br/> system. out. println (newstr); <br/> // judge the start and end of a string: <br/> string num1 = "22045612"; <br/> string num2 = "21304578 "; <br/> Boolean B = num1.startswith ("22"); <br/> Boolean b2 = num1.endswith ("78 "); <br/> Boolean B3 = num2.startswith ("22"); <br/> Boolean B4 = num2.endswith ("78"); <br/> SY Stem. Out. println ("does the string num1 start with '22? "+ B); <br/> system. Out. println (" is the string num1 ending with '78? "+ B2); <br/> system. Out. println (" does the string num1 start with '22? "+ B3); <br/> system. Out. println (" is the string num1 ending with '78? "+ B4); <br/> // judge whether the string is equal <br/> string Tom = new string (" I am "); <br/> string Jerry = new string ("I am"); <br/> Boolean BB = (Tom = Jerry); <br/> Boolean BBB = Tom. equals (Jerry); // specify signorecase <br/> system. out. println ("Tom = JERRY:" + BB); <br/> system. out. println ("Tom. equals (Jerry): "+ BBB); <br/> // case-sensitive conversion: <br/> string mystr = new string (" ABC Def "); <br/> string newstrs = mystr. tolowercase (); <br/> string newstrs2 = mystr. touppercase (); <br/> system. out. println (newstrs); <br/> system. out. println (newstrs2); <br/> // string segmentation: <br/> string splitstr = new string ("ABC, def, Ghi, gkl "); <br/> string [] newsplitstr = splitstr. split (","); <br/> for (INT I = 0; I <newsplitstr. length; I ++) <br/>{< br/> system. out. println (newsplitstr [I]); <br/>}< br/> string [] newsplitstr2 = splitstr. split (",", 2); // limit the number of splits <br/> for (INT I = 0; I <newsplitstr2.length; I ++) <br/>{< br/> system. out. println (newsplitstr2 [I]); <br/>}< br/> // format the string: <br/> // format the date <br/> // % te a day in a month (1-31) <br/> // % TB month <br/> // full name of % TB month <br/> // % ta full name of the day of the week <br/> // % ta weekly <br/> // % tc <br/> // % ty 4-digit year (2009) <br/> // % the day of the TJ year (001-366) <br/> // % TM month <br/> // % TD day of the month (01-31) <br/> // % ty two-digit year <br/> date = new date (); <br/> string dates = string. format ("% te", date); <br/> string datedays = string. format ("% TJ", date); <br/> system. out. println ("Date:" + dates); <br/> system. out. println ("day of the year:" + datedays); <br/> // time format: <br/> // % Th 2-digit 24 hours: (00-23) <br/> // % Ti 2-digit 12 hours :( 01-12) <br/> // % TK 2-digit 24 hours :( 0-23) <br/> // % TL 2-digit 12 hours :( 1-12) <br/> // % TM 2-digit minute (00-59) <br/> // % ts 2-digit second (00-60) <br/> // % TL 8-digit Millisecond (000-999) <br/> // % TN 9-bit Millisecond () <br/> // % TP last afternoon, <br/> // % TZ <br/> // % ts 00:00:00 seconds passed so far <br/> // % TQ 00:00:00 milliseconds passed so far <br/> string hour = string. format ("% th", date); <br/> string minute = string. format ("% TM", date); <br/> string second = string. format ("% ts", date); <br/> string msecond = string. format ("% TQ", date); <br/> system. out. println ("now" + hour + "+ minute +" Minute "+ second +" second "); <br/> system. out. println ("now in milliseconds:" + msecond); <br/> // Date and Time combination: <br/> // % TF year-month-day <br/> // % TD month/day/year <br/> // % TC all date and time information <br/> // % tr hours/Seconds <br/> // % TT hours/Seconds <br/> // % tr hours <br/> string time = string. format ("% TC", date); <br/> string form = string. format ("% TF", date); <br/> string SFM = string. format ("% TT", date); <br/> system. out. println ("all time information:" + time); <br/> system. out. println ("year-month-day format:" + form); <br/> system. out. println ("minutes and seconds:" + SFM); <br/> system. out. println ("date. tostring (): "+ date. tostring (); // Date. tolocalestring () <br/> system. out. println ("date. tolocalestring (): "+ date. tolocalestring (); <br/> // use a regular expression <br/> // string builder: stringbuilder <br/> // builder. append (); insert (INT offset Arg); Delete (INT start, int end); <br/> stringbuilder BF = new stringbuilder ("hello "); <br/> BF. insert (5, "word"); <br/> system. out. println (BF. tostring (); <br/>}</P> <p>}

 

From http://blog.csdn.net/dingdangxiaoma/archive/2010/08/24/5835759.aspx

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.