Java (vi, String class, and StringBuffer)

Source: Internet
Author: User

Java string class strings are widely used in Java programming, where strings belong to objects, and Java provides the string class to create and manipulate strings. Create a string
= = compares the first address the string holds in the stack, while Equals () compares the contents of the two strings.
     //The normal method of declaring a string will only open up space in the string constant pool, and will check if the string constant pool exists before opening space .//the same data, if any, points directly to the existing data, without opening a new space in the string constant poolString s = "Ccy"; String S2= "Ccy"; System.out.println (S==S2); System.out.println (s.equals (S2)); //instantiating a method that declares a string first stores the data in the heap, saves the first address of the data in the stack, and then checks to see if the string constant pool exists//the same data, if not, opens up a new space in the string constant pool to hold the string data, and if so, the declaration is complete. String s3 =NewString ("jredu"); String S4=NewString ("jredu"); System.out.println (S3==S4); System.out.println (S3.equals (S4)); String S5= "Jereh"; String S6=NewString ("Jereh"); System.out.println (S5==S6); System.out.println (S5.equals (S6));
Operation diagram:

Example diagram of how strings are stored in memory space:

The string method: 1, the length of strings used to get information about an object is called an accessor method. An accessor method of the string class is the length () method, which returns the number of characters that the string object contains.
New String ("string Length"); int length = string.length (); System.out.println (length);
Operation diagram:

2. The connection string class provides a method for connecting two strings: 1, String1.concat (string2), and a new string that returns string2 connection string1. You can also use Concat () method 2 for string constants, and more commonly use the ' + ' operator to concatenate strings below is an example:
 Public class Demo06 {    publicstaticvoid  main (string[] args) {        new String ("Zhang San,");         New String ("Hello!" );         = S.concat (hello);        System.out.println (sentence);    }}
Operation diagram:

3, split string array string.split (parameter), the parameter refers to from which character segmentation example:
 Package Day6; Import java.util.Arrays;  Public class Demo09 {    publicstaticvoid  main (string[] args) {        = "changting outer trail Edge" Grass Green Breeze Breeze Liudisheng Sound remnant Sunset Mountain outside Mountain ";        String[] printsing;         = Sing.split ("");        System.out.println (arrays.tostring (printsing));}    }
Operation diagram:

4, toUpperCase () make the lowercase string into uppercase string toLowerCase () make uppercase string into lowercase string equalsignorecase () string Ignore case for comparison example:
 Public class Test {    publicstaticvoid  main (string[] args) {        = "abc";        = "ABC";         = s1.touppercase ();         = s2.tolowercase ();        System.out.println (S3);        System.out.println (S4);        System.out.println (S1.equals (S4));        System.out.println (S2.equals (S3));        System.out.println (s1.equalsignorecase (S2));}    }
Operation diagram:

5, other common methods A, indexOf () Gets the position of a character or string in a string for the first time, and returns -1B, LastIndexOf () to get the last occurrence of a character or string in a string if it does not appear, or return -1c, SUBSTRING () starts from the index of the string, obtains a new string, the first parameter of the two parameter is the position where the Intercept begins (contains), the second parameter is the end position (not included) d, trim () remove the string or the space before and after the character example:
 Public classDemo08 { Public Static voidMain (string[] args) {String s= "I love Beijing Tian ' an gate!" The sun rises on Tiananmen Square! "; //indexOf () Gets the position of the first occurrence of a character or string in a string, or 1 if it does not appear .System.out.println (S.indexof ("Day")); //lastIndexOf () Gets the last occurrence of a character or string in a string and returns 1 if it does not appearSystem.out.println (S.lastindexof ("Day")); //substring () starts from the index of the string and gets a new stringString NewS = s.substring (8);        System.out.println (NewS); //the first parameter of the two parameter is the position (inclusive) where the intercept begins, and the second parameter is the end position (not included)String newS3 = s.substring (2, 4);        System.out.println (newS3); //trim () remove space before or after a string or characterString s2 = "Jerry Education"; String newS4=S2.trim ();        SYSTEM.OUT.PRINTLN (S2);    System.out.println (NEWS4); }}
Operation diagram:

StringBuffer class when modifying a string, it is necessary to use the StringBuffer and string classes, where objects of the StringBuffer class can be modified multiple times without creating new unused objects. Examples of StringBuffer creation:
New StringBuffer ("Youth Without Regrets"); System.out.println (sbuffer);

Operation diagram:

StringBuffer and string differences: Java StringBuffer and string is a certain difference, first of all, the string is final modified, his length is immutable, even if the concat method called String, it is The string is stitched together and recreated to create an object that assigns the value of the stitched string to the newly created object, while the length of the StringBuffer is variable, calling StringBuffer's Append method to change the length of the StringBuffer, and, compared to Stringbuffer,string in the event of a change in length, it is very memory-intensive! Conversion between StringBuffer and string: Example:
 Public Static void Main (string[] args) {        = "Conversion"        //String---> StringBuffer        New  StringBuffer (s);         // stringbuffer---> String        String s2 = sb.tostring ();        System.out.println (SB);  SYSTEM.OUT.PRINTLN (S2);        Scanner.close ();    }

Operation diagram:

Common methods of StringBuffer: A, append (string s) append the specified string to this sequence of characters.
 Public Static void Main (string[] args) {        new stringbuffer ("ABCDE");        Buffer.append ("F");        SYSTEM.OUT.PRINTLN (buffer);}

Operation diagram:

b, reverse () replaces this sequence of characters with its inverse form.
 Public Static void Main (string[] args) {        new stringbuffer ("ABCDE");        Sbuffer.reverse ();        System.out.println (Sbuffer);    }

Operation diagram:

c, delete (int start, int end) Removes the characters from the substring of this sequence.
 Public class Demo04 {    publicstaticvoid  main (string[] args) {        new StringBuffer ("ABCDE");        Sbuffer.delete (1, 3);        System.out.println (Sbuffer);    }
Run diagram: D, insert (int offset, int i) inserts the string representation of the int parameter into this sequence.
 Public Static void Main (string[] args) {        new stringbuffer ("ABCDE");        Sbuffer.insert (2, "!" );        System.out.println (Sbuffer);    }

Operation diagram:

E, replace (int start, int end, String str) replaces the characters in the substring of this sequence with the characters in the given string.
 Public Static void Main (string[] args) {        new stringbuffer ("ABCDE");        Sbuffer.replace (1, 4, "*");        System.out.println (Sbuffer);    }

Operation diagram:

Java (vi, String class, and StringBuffer)

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.