Dark Horse programmer--14,string related knowledge points

Source: Internet
Author: User

------<ahref= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Dark Horse programmer--14,string related knowledge points

/*

The following is a knowledge point of the string class-related methods

*/

Class zifuchuan{public static void Main (string[] args)//main function {String a= "Yiersan";//Here's A is a reference Type variable, and "Yi" is an object/* This sentence has only one object in memory the "Yiersan" string is not changed once it has been initialized * /String B=new string ("Yiersan");//This sentence has two objects in memory one of which is "Yiersan" and the other is the new string () string c= "Yiersa        n ";//Here the C is the same object as the previous a," Yiersan "/* Because once the string is initialized it will not be changed, and the object" Yiersan "is already defined in memory.       */String d= "Yiersansi";       System.out.println ("a==b----" + (a==b));       System.out.println ("a==c----" + (A==C));                   System.out.println ("a==d----" + (A==d));                   System.out.println ("a.equals (b)----" +a.equals (b));                    /* equals This method is originally defined in the object class to compare the address value but this method is replicated in the string class, the function becomes the comparison string content is the same. */}}/* The result of the code compilation Run as follows: A==b----false A==c----true A==d----false a.equals (b)----true */

—————— Split Line ————

/*

There are many useful methods defined in string.

Here are some common methods for the string class

For illustrative purposes, give a specific example

*/

Class zifuchuan2{public static void Main (string[] args)//main function {String a= "Yiersansiwuliou"; The corners in the string are incremented from 0 to the SOC (A.length ());//This statement prints that the//a.length () returned is the length of the string Soc (A.C Harat (5));//This sentence prints A/* A.charat (5) that returns the character//SOC (A.charat (50)) in the string with a corner labeled 5;//This sentence will appear        An exception to the bounds of the angular mark */SOC (A.indexof (' I '));//This sentence is printed in 1//A.indexof (' I ') returns the corner label of the first occurrence of the ' I ' in the string                   Soc (A.indexof ("IW"));//This sentence is printed with 8 Soc (A.indexof (' I ', 4));//This sentence prints 8/*       The function of A.indexof (' I ', 4) is to search for the first occurrence of the character ' I ' in the string, starting with the character in the corner labeled 4, and then return the corresponding corner mark.                    */SOC (A.indexof ("IW", 4));//This sentence is printed with 8 Soc (A.indexof (' d ', 4));//This sentence is printed-1 A.indexof (' d ', 4) This sentence does not find the character you are looking for will return-1//The following is the reverse search: Soc (a.lastindexof (' I ', 6));//This sentence prints 1 Soc (a . lastIndexOf (' I '));//This sentence hitsPrinted is a SOC (A.lastindexof ("IW")),//This sentence is printed on the 8 Soc (A.lastindexof ("wi"));//This sentence is printed 1             Soc (A.lastindexof ("IW", 11));//This statement prints the 8} public static void Soc (Object obj) {         System.out.println (obj); }}

—————— Split Line ————

/* or introduce some common methods in the String class */class zifuchuan3{public static void Main (string[] args)//main function {Strin       G a= "Yiersansiwuliou";                   String b= "";       String c= ""; Soc (A.isempty ());//This statement prints false//a.isempty () function: detects if the string is content, or returns False if True otherwise the SOC (B.isempty ());//This                   The word printed is True Soc (C.isempty ());//This sentence is printed with a false Soc (A.startswith ("Yi"));//This sentence is printed true The A.startswith ("Yi") function is to detect if a string is preceded by Yi, and if true otherwise returns False Soc (A.startswith ("Y"));//This statement prints true so C (A.endswith ("ou"));//This statement prints true//a.endswith ("ou") function is to detect whether a string is terminated with an OU, or return true otherwise false//soc (a.end Swith (' u '));//This sentence is compiled without the SOC (A.contains ("San"));//This statement prints true//a.contains ("san") function is to detect whether the string contains a SAN, if is to return true otherwise returns false//soc (A.contains (' s '));//This phrase is compiled without the SOC (a.equals ("San"));//This sentence is printed with false//a.               The Equals ("San") function is to detect whether the string content is equal to the SAN, or return True otherwise false    The Equals method is originally the method in the object class, is to determine whether the address value is equal, here is a copy of the SOC (A.equalsignorecase ("Yiersansiwuliou"));//This sentence is printed true /* A.equalsignorecase ("Yiersansiwuliou") function is: case-insensitive detection string is equal to Yiersansiwuliou, if return True otherwise returns false */} public static void Soc (Object obj) {SYSTEM.O         Ut.println (obj); }}

—————— Split Line ——————

/*

The corresponding conversion method or constructor for the string class

For the convenience of understanding, direct use of examples to illustrate

*/

Class zifuchuan4{public static void Main (string[] args)//main function {char[] a={' y ', ' I ', ' e ', ' R ', ' s ', ' a       ', ' n ', ' s ', ' I ', ' w ', ' u ', ' l ', ' I ', ' o ', ' u '};       This is a character array containing a bunch of characters byte[] b={2,25,46,85,76,95};                   String c= "Qibajioushi";         String d= "25648612";                   Convert a character array to a string using a constructor in the string class: String Stra=new string (a); String a bunch of characters in the character array A in the order of strings "Yiersansiwuliou", and the reference variable Stra point to the String Soc (stra);//print "Yiersansiwuliou" string stra2=n                   EW String (a,3,9);                  The character array A in the beginning of the corner 3 characters in sequence string, the length of the string is 9, and then with the reference variable STRB point to the String Soc (STRA2);//print "Rsansiwul"       Converts a byte array to a string: Soc (new string (b));//Note: Print a string that is not a number, but a symbol of the corresponding encoding table!!!                   Soc (new String (b,2,4));//Note: printing is not a string of numbers, but the corresponding code table symbol!!! The corresponding encoding table can be customized//with a static method in the string class to convert the character array to a string: Soc (String.valueof (a));//print "Yiersansiwuliou" Soc (S Tring.copyvalueof (a));//print "YiersAnsiwuliou "Soc (string.copyvalueof (a,3,9));//" Rsansiwul "//can also put numbers into the SOC (string.valueof (12));//Print 12 Soc (string.valueof (15.4));//Print 15.4//soc (string.copyvalueof (b,2,4));//This sentence compilation does not pass char[] shuzhuc=c.t                                      Ochararray ();//Convert the string C into a bunch of character arrays for (int x=0;x<shuzhuc.length;x++) {                               Soc ("shuzhuc=" +shuzhuc[x]);//print character array Shuzhuc} byte[] Shuzhud=d.getbytes (); for (int x=0;x<shuzhud.length;x++) {Soc ("shuzhud=" +         SHUZHUD[X]);//print character array Shuzhud}} public static void Soc (Object obj)//soc printing method         {System.out.println (obj); }/* the results of the above code compilation Run as follows: Yiersansiwulioursansiwul[1]. Ul_. Ul_yiersansiwuliouyiersansiwulioursansiwul1215.4shuzhuc=qshuzhuc=ishuzhuc=bshuzhuc=ashuzhuc=jshuzhuc=ishuzhuc= Oshuzhuc=ushuzhuc=sshuzhuc=hshuzhuc=ishuzhud=50shuzhud=53shuzhud=54shuzhud=52shuzhud=56shuzhud=54shuzhud=49shuzhud=50*/ 

———————— Split Line ——————

/*

The following describes the substitution and cutting methods in the String class

*/

Class zifuchuan5{public static void Main (string[] args)//main function {String a= "Yiersansiwuliou"; String B=a.replace (' I ', ' k ');//Replace the character I in the string with the character K string c=a.replace ("Yier", "Dada");//String The string yier in is replaced with the string Dada Soc ("a=" +a);//The A=yiersansiwuliou Soc ("b=" +b);//Print b=                    Ykersanskwulkou//Because the string cannot be changed once it has been initialized, this happens//real memory has two objects in it.                      Soc ("c=" +c);                    The following is an introduction to cutting: System.out.println ("Cut print------" below);                    String[] sza= a.split (' i ');//This sentence compilation does not pass, the parentheses can not put characters, can only put the string string[] Sza=a.split ("i");//This sentence is compiled through the                    for (int x=0; x<sza.length; x + +) {Soc ("Sza---" +sza[x]);//Printing individually                    }//soc ("array sza length =" +sza.length);//This sentence is printed with an array sza length = 4 *//The following is about cutting another little bit of knowledge String op= "JKHSDJKSDBFJKBWDGW ";                    String[] Szop=op.split ("JK");        Soc ("array szop length =" +szop.length);//This sentence prints the array szop length =4 String op2= "JKHSDJKHSDJKBWDGW";                    String[] Szop2=op2.split ("JK"); Soc ("array szop2 length =" +szop2.length);//This sentence prints the array szop2 length =4 */string[] sza2= a.spli                    T ("IW");                    for (int x=0; x<sza2.length; x + +) {Soc ("SZA2---" +sza2[x]);//Printing individually                    } System.out.println ("-------below is the fetch--------------------of the string"); String d= a.substring (4);//Gets the string in the string after the beginning of the corner label 4, which is obtained not cutting the SOC (d);//printing is Sansiwuliou Strin                                G e= a.substring (4,9);//Gets the string from the angle label 4 to the corner 9, but does not contain the character Soc (e) of the corner label 9;//Print is Sansi         } public static void Soc (Object obj)//soc printing method {System.out.println (obj); }/* the results of the above code compilation Run as follows: A=yierSansiwulioub=ykersanskwulkouc= Dadasansiwuliou below is the cut print------sza---ysza---ersanssza---wulsza---ousza2---yiersanssza2---uliou-------The following is the acquisition of the string-------------- ------Sansiwuliousansi * *

———————— Split Line ——————

/*

Convert case in string class, remove front and back whitespace, compare

*/

Class zifuchuan6{public static void Main (string[] args)//main function {String a= "Yiersansiwuliou";                    String a2= "Yiersansi Wu Liou";                    Soc ("a=" +a+ "--a2=" +a2); String B=a.touppercase ();//convert string Yiersansiwuliou to uppercase and return uppercase String C=b.tolowercase ();//String Yiersansiwul                    IOU turns lowercase and returns a lowercase string soc ("b=" +b);        Soc ("c=" +c);                            String E=a2.trim ();//Return to remove the front and back space of the strings of the SOC ("e=" +e);                    String f= "ASD";                    String g= "Ahj";                              int h= F.compareto (g);//returns an int type variable/* For convenience of explanation, I remember F as the former, G for the latter f and G compared, first starting from the first place to compare,                              Then the first time you encounter a different character, compare these two different characters to the numeric size of the encoded table in this example, the first bit is a                      Then the second bit of the former is the second bit of the latter is H compared to the second character corresponding to the size of the encoded table, the number of characters in the following digits         If the former corresponds to the encoding table value is greater than the latter returns a positive number, if the same return 0, otherwise return negative */SOC (h);         } public static void Soc (Object obj)//soc printing method {System.out.println (obj);  }/* above code compile run result: a=yiersansiwuliou--a2= yiersansi wu Liou b=yiersansiwuliouc=yiersansiwulioue=yiersansi Wu Liou11 * *

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse programmer--14,string related knowledge points

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.