-------------------- Android training, java training, and hope to communicate with you!
String
The following descriptions are provided in the API documentation:
Public final class String extends Object implements Serializable, Comparable <String>, CharSequence
It can be seen that Stirng is not inherited.
String has the following features:
Fixed Length. Once generated, it cannot be changed.
Code explanation:
String str1 = "abc ";
Str1 = "def ";
The two lines of code do not change the string. the string "abc" has not changed, but str1 has changed from pointing to "abc" to pointing to "def ".
To change the character sequence, use StringBuffer or StringBuilder.
StringBuffer
In the API documentation, StringBuffer is described as follows:
Public final class StringBuffer extends Object implements Serializable, CharSequence
It can be seen that StringBuffer is not inherited, and it is a thread-safe class, that is, its internal methods are mutually exclusive.
The stringbuffer class provides append, insert, delete, deletecharat, setcharat, and other methods to change the current stringbuffer object.
Stringbuilder
In the API documentation, the description is as follows:
Public final class stringbuilder extends object implements serializable, charsequence
Likewise, it cannot be inherited, and its methods are almost the same as stringbuffer. Different from stringbuffer, stringbuilder is thread-safe and does not have a thread synchronization guarantee mechanism.
Some methods of string are implemented in the course of instructor Bi Xiangdong's lecture. The following are some of my methods:
// It is used to imitate the trim method in the string class and remove spaces before and after the string. Public String simtrim (string Str) {stringbuffer sb = new stringbuffer (STR); While (sb. charat (0) = '') {sb. deletecharat (0);} while (sb. charat (sb. length ()-1) = '') {sb. deletecharat (sb. length ()-1);} return sb. tostring ();}
In this method, the stringbuffer class Sb can be changed to stringbuilder.
// This method is used to calculate the substrings in the parent String. public int getTimes (String mager, String sub) {int times = 0; if (mager. length () <sub. length () {} else {for (int I = 0; I + sub. length () <= mager. length (); I ++) {if (mager. substring (I, I + sub. length ()). equals (sub) {times ++ ;}} return times ;}
Unlike the Code implemented by teacher Bi Xiangdong, if the mother string is "kkk" and the Child string is "kk", the result of the instructor bi's method is 1, the result of my implementation is 2, because I think the first two kk and substring in the parent string are the same, and the last two kk are also the same as the substring, so we should calculate two.
------------------------------------------ Android training, java training, we look forward to communicating with you!
----------------------
See http://edu.csdn.net/heima for details