4, the various operation of the string

Source: Internet
Author: User

 Packagecom.xxx.xxx;Importjava.util.Date; Public classDemo5 {/*** String Connection *@paramargs*/     Public Static voidMain (string[] args) {//String Connection        intA = 33; floatb = 44.5f; //when a string is concatenated with an shaping, floating-point variable, the toString () method is automatically called, and the connection is joined after it is converted to a stringSystem.out.println ("I Spend" +a+ "hours a day reading," +b+ "Hours of computer practice"); //gets the string length str.length ()String S1 = "Hello"; intSize =s1.length (); System.out.println ("Length is:" +size); //string Lookup starting from 0 indexOf (string s);        intA1 = S1.indexof ("o"); System.out.println ("Character O first occurrence position:" +A1); intA2 = S1.lastindexof ("e"); System.out.println ("Character E last seen position:" +A2); //Gets the character Str.charat () of the specified index;String str = "Hello word!"; CharMyChar = Str.charat (6); System.out.println ("The character in the string str with index position 6 is:" +MyChar); //1.1 get substring substring (int brgindex)String substr = str.substring (3); System.out.println ("1.1 Get substring:" +substr); //1.2 get substring substring (int brgindex,endindex)String substrend = str.substring (0, 3); System.out.println ("1.2 Get substring:" +substrend); //Remove Spaces Str.trim () leading and trailing spacesString str1 = "Good bye! "; String Strtrim=Str1.trim (); System.out.println ("Source string Length:" +str1.length () + "---" +str1+ "length after removing spaces:" +strtrim.length () + "----" +Strtrim); //string Substitution str.replace (char Oldchar,cahr Newchar) converts the specified string to a new stringString str2 = "adress"; String newstring= Str2.replace ("A", "good"); System.out.println ("Source string:" +str2+ "new string:" +newstring); //to determine the start and end of a string the return value is a Boolean str.startswith (string prefix) endwith (string suffix)String STR3 = "ABCDEFG"; BooleanBool1 = Str3.startswith ("a"); BooleanBool2 = Str3.endswith ("G"); System.out.println (Bool1+"  "+bool2); //determines whether strings are equal str.equals (string other) case-sensitive str.equalsnorecase ( string Other)String SS1 =NewString ("ABC"); String SS2=NewString ("ABc"); System.out.println (equals case-sensitive string comparison 1: "+ss1.equals" ("ABC")); System.out.println ("Equals case-sensitive string comparison 2:" +ss1.equals (SS2)); System.out.println ("Case-Insensitive string comparison 2:" +ss1.equalsignorecase (SS2)); //compare two strings in dictionary order Str.compareto (string other)System.out.println (Ss1.compareto ("Def"))); //Letter Case conversion (numbers and non-characters not affected) Convert lowercase s3.tolowercase () convert uppercase S3.touppercase ()String s3 = "ABc"; System.out.println ("Original:" +s3+ "converted to lowercase" +s3.tolowercase ()); System.out.println ("Original:" +s3+ "converted to uppercase" +s3.touppercase ()); //string Split split ( string sign)String S4 =NewString ("Abc,deg,ghi,gki"); String s5arr[]= S4.split (",");//no split number set                 for(String Aa1:s5arr) {System.out.println ("" No set number of splits "before the split:" +s4+ "after the split string:" +Aa1); } String s6arr[]= S4.split (",", 2);//set Number of times is 2         for(String Aa2:s6arr) {System.out.println ("" Set the number of splits 2 times before the split: "+s4+" after the split string: "+AA2); }                //formatted string format ()%te%th%tl Baidu commonly used date format conversion characterDate Date =NewDate (); String Time= String.Format ("%TC", date); String form= String.Format ("%TF", date); String s_date= String.Format ("%te", date); System.out.println ("Formatted string:" +s_date); System.out.println ("All the Time information:" +Time ); System.out.println ("Year-month-day format:" +form); //Format () (There is also a general type of formatting%b%d%e ... )String a10 = String.Format ("%d", 400/2);//decimalString A11 = String.Format ("%b", 400/2);//Boolean typeString A12 = String.Format ("%x", 400/2);//Hexadecimal typeSystem.out.println ("400/2 Decimal type format:" +a10+ "Boolean Type type format:" +a11+ "hexadecimal type format:" +A12); }}

4, the various operation of the string

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.