Some common methods of JAVA String and StringBuffer

Source: Internet
Author: User


String

The constant pool concept, when the string object is new and is referenced as: string s = new string ("Hello");//It opens an address in the stack, the reference address is in the stack, and the value is stored in the heap;
When string is passed to: string s = new string;s= "Hello";//its address in the constant pool is not equal

The string class is located in the Java.lang package and is rich in methods: calculating the length of a string, comparing strings, connection strings, extracting strings

The. Touooercase () string is converted to uppercase;. toLowerCase () returns a lowercase string;. equalsignorecase (), to determine ignoring case

. concat s = s.concat (' xxxx ') string concatenation, S + = ' xxxx ' can also realize the concatenation of strings;

. Contains ("X") determines whether the string contains X and returns a Boolean value;

. IndexOf () The position of the first occurrence; LastIndexOf () the last occurrence;

. substring (int a) extracts the contents of paragraph a after;. substring (int a,int b) extracts the contents of items A to B; (Overloading of methods)

. Trim () Returns a copy that does not contain any space characters before or after

. Split () intercepts a character and separates it into a new array;

. ToCharArray () string into a character array


Stringbuffer:string Enhanced Version

Reference data types require a new build object, StringBuffer sb = new StringBuffer ()
Assigning values to objects (adding characters)
Data processing
Use complete, set length=0;

. toString (); Convert to String type

. Append ("xxx") Append string

. insert (int A, "x") A is the position that needs to be added to the subscript a, and X is a string that can add data of any data structure

. Recerse () string content upside down

. Delete (start, end) deletes from the start position to the end position;

. replace (start, End, "X") replaces the start position to the end position with X

. replace ("a", "X") replaces all of a with x;

. SetLength () Sets the length of the string, typically used for word Fu Qing 0, to free up memory space

Some common methods of JAVA String 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.