Java Learning Lesson 29th (often using object APIs)-String class

Source: Internet
Author: User

Multithreading to the beginning, often use the object API involved, back also to back down!!!

Future development, meet the most object is the text, that is, the string

String class

A string is a special object

Once the string is initialized, it cannot be changed.


First, the characteristics

public class Main {public static void main (string[] args) {Demo1 (); System.out.println ("--------------");D Emo2 ();} /* Demonstrates the first definition of a string and understands the character of the string constant pool */private static void Demo1 () {string S1 = "ASD"; s1 = "SD";//s1 is simply a reference to the string, and the string "ASD" itself does not change the string s2 = new String ("SDF"); System.out.println (s1); s2 = "SD"; System.out.println (S2==S1);//s1 and S2 actually point to the same address value//sd stored in the buffer (string constant pool)//assumed not. Create a different way of defining the}/* demo string without creating it, */public static void Demo2 () {String s = "abc";//Create an object in a string constant pool string S1 = new String ("abc"); No object is created in the string constant pool, two objects are created in heap memory///One is S1 an "ABC" System.out.println (s = = S1);//false, the comparison of the address value is not the same System.out.println ( S.equals (S1)); equals in the//true/*string class has a copy of equals in object that establishes the string class * its own inferred string object is the same as the string content */system.out.println ("s =" +s); System.out.println ("S1 =" +s1);}}

Second, the structure function

public class Main{public static void Main (string[] args) {StringconstructorDemo1 (); Chardemo ();} public static void StringconstructorDemo1 () {string s = new string ();//equivalent to string s = ""; Address value not equal, string s = nullbyte[] s UB = {97,66}; string S1 = new String (sub); System.out.println ("S1 =" +s1);} public static void Chardemo () {char[] ch = {' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; String string = new string (ch);//convert array to string, only Byte and charSystem.out.println ("string =" +string); String string1 = new string (ch,0,3);//3 characters System.out.println from 0 corners ("string1 =" +string1);//string class remaining method, check manual}


Iii. methods involved in strings

Fully Code-based demo

/* Requirements: * Learn the string based on object-oriented thinking * "ABCDEFG" * 1. The length of the string * 2.d in the position of the string * 3 ..... * */public class Main{public static void Main (string[] args) {//stringmthoddemo1 ();//Get//stringmthoddemo2 ();//Convert// StringMthodDemo3 ();//infer StringMthodDemo4 ();//compare}public static void StringMthodDemo1 ()//One, get {String str = "Abcdefghijk ";/* 1. Gets the string length int length () */int len = Str.length ();  System.out.println ("str len =" +len);/* 2. Gets the character char charAt (int index) based on the position */char wz = Str.charat (3); System.out.println ("character indexed to 3" +WZ);/* 3. Gets the character's position according to the character indexOf (emphasis) */int IWZ = Str.indexof (' x ');//write ' A ' also line, write 97 also line (get the position of the first occurrence of the session) System.out.println ("position of a" +iwz); /-1 indicates that there is no//3.1 overloaded form int iwz1 = Str.indexof (' d ', 2);//Returns the index at the first occurrence of the specified string in this string. Searches for System.out.println ("D in string position" +IWZ1) from the specified index,//3.2 overloaded form int iwz2 = Str.indexof ("CD");// Returns the index SYSTEM.OUT.PRINTLN of the specified string at the first occurrence of the specified string in this string ("where the string CD first appears" +IWZ2),//3.3int iwz3 = Str.indexof ("CD", 1);//  LastindexOf ()//returns the index at the last occurrence of the specified string in this string, as above (must be)/* 4. Intercept partial substring substring (int beginindex,int beginindex) */stRing Sub = str.substring (0,3),//Sub-string System.out.println from Beginindex to "BeginIndex-1" ("sub-string index from 0-2" +sub);//.... Manual}public static void StringMthodDemo2 ()//second, convert {String str = "Abc,efg,hij,lmn,opq,rst";/* 1. Turn the string into an array of strings (that is, cut the string into segments) split */string[] arr = Str.split (",");//with "," for rule cutting. refers to the normal table for (String I:arr) {System.out.println (i);} /* 2. Converts a string into a character array (cut into a single character) tochararray*/char[] arr1 = Str.tochararray (); for (char i:arr1) {System.out.println (i);}  /* 3. Converts a string into a byte array etbytes*/string str1 = "abc Assad";//abc 3 bytes. Three characters 6 bytes//ascii is the United States, China also has its own GB2312 corresponding binary code table byte[] g = str.getbytes (); for (byte i:g) {System.out.println (i);} /* 4. Uppercase and lowercase letters convert touppercas */string str2 = "ASD"; String STR3 = "AAAA"; string T1 = Str2.touppercase ();//variable capitalization string t2 = Str3.tolowercase ();//Variable lowercase System.out.println ("Up" +t1 + "++++" + "Lower" +t2  );/* 5. Character replacement replace */string STR4 = "ASFSD"; String t3 = str4.replace (' s ', ' a ');/' If s does not exist. There is no result. Returns the original string System.out.println ("character substitution" +t3); String T4 = str4.replace ("asf", "AAA"); System.out.println ("substring substitution" +t4); /* 6. Remove both ends of the string trim (practical) *///For example texting 10086 sometimes habitually press a space, but this space is not supported. So trim removes this space System.out.println ("ABCD". Trim ());/* 7.  Connect the string to */system.out.println ("ASD") concat ("ASD");//concat's core code is "ASD" + "ASD"/* 8. */system.out.println (string.valueof (25) +0);//convert 25 to string and 0 connection, println ("" +25+0); public static void StringMthodDemo3 ()//third, inferred {/* 1. Equals */string Str1 = "ASD"; String str2 = "ASD"; Boolean flag = Str1.equals (STR2);  SYSTEM.OUT.PRINTLN (flag);/* 2. Equalsignorecase */system.out.println (Str1.equalsignorecase (STR2));//ignore the effect of uppercase and lowercase on strings//equivalent to Str1.equals (  Str2.tolowercase ()); Of course worse/* 3. Whether the string contains the specified string Contains*/system.out.println ("Asdfghj". Contains ("FGH"));/* 4. Whether the string begins or ends with the specified string StartsWith endsWith */system.out.println ("ASDFGHJK". StartsWith ("ASD") + "..." + "ASDFGHJK". EndsWith ("JKL"));/* */}public static void StringMthodDemo4 ()//four, Comparison {//According to the dictionary order comparetostring str1 = "abc"; String str2 = "EFG"; System.out.println (Str2.compareto (STR1));//equals 0:str1 equal str2, greater than 0:str1 greater than str2, less than 0:str1 less than str2}} 

It's all dead stuff. Back to the back, too.





Java Learning Lesson 29th (often using object APIs)-String class

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.