Common methods for Java string algorithms (continuous update)

Source: Internet
Author: User

String,stringbuilder,stringbuffer

The difference between the three string,stringbuilder,stringbuffer in Java

1. At run Speed: StringBuilder > StringBuffer > String
Cause: Strings are string constants, and StringBuilder and StringBuffer are string variables, which are immutable after a string object is created, but the latter object is a variable and can be changed. For example:

String str = "ABC"; System.out.println (str); str = str + "de"; System.out.println (str);

In the third row, Str appears to have been modified, in fact, the JVM in the third row created a new object also named Str, and then the original object's str value and "de" added to the new STR, and the original STR will be garbage collection mechanism of the JVM to be recycled, So the STR here is not actually changed.

2. On-thread security, StringBuilder threads are unsafe, and stringbuffer is thread-safe
If a StringBuffer object is used by multiple threads in a string buffer, many methods in StringBuffer can have synchronized keywords, so the thread can be secured, but the StringBuilder method does not have that keyword. Therefore, thread safety is not guaranteed. So if you use multi-threading then use StringBuffer, but it is recommended to use StringBuilder faster in a single thread.

Common methods of StringBuilder:
1. Create a StringBuilder object: StringBuilder str = new StringBuilder ();
StringBuilder str = new StringBuilder (string);
2. Append: String Connection str.append ("AAA");
3. toString:str.toString ();
4. Setcharat (int i, char c): Replace position I with C
5. Insert (int I, string str): Inserts a character or string at the specified position
6. Delete (int start, int end): Deletes the string between the start position (including) to the end position (not included)
7. Replace (int start, int end, string str): Replace with str from start to end

Common methods of StringBuffer:
? Add features
Public stringbuffer Append (String str): You can add any type of data to the string buffer and return the string buffer itself
Public stringbuffer Insert (int offset,string str): Inserts any type of data into the string buffer at the specified location and returns the string buffer itself
? Delete Feature
Public stringbuffer deletecharat (int index): Deletes the character at the specified position and returns itself
Public stringbuffer Delete (int start,int end): Deletes the end of the specified position starting at the specified position and returns itself
? Replace function
Public stringbuffer replace (int start,int end,string str): Replace with str from start to end
? reversal function
Public StringBuffer Reverse ()
? interception function
Public String substring (int start)
Public String substring (int start,int end)

Common methods for Java string algorithms (continuous update)

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.