Commonly used method in Java StringBuffer class

StringBuffer:StringBuffer类型

Description: In practical applications, often encountered in the string back to the dynamic changes. At this time, String class functions are limited, and can be completed dynamically added StringBuffer class string, insert and replace operations.

1, the constructor.

StringBuffer() :构造一个没有任何字符的StringBuffer类。
StringBuffer(int length) ::构造一个没有任何字符的StringBuffer类,并且,其长度为length。
StringBuffer(String str) :以str为初始值构造一个StringBuffer类。

2, method.
Description:

  1. All methods are public;
  2. Writing format: [Modifier] <return type> <method name ([parameter list])>
    such as:
    static int the parseInt (String S) represented by: This method (the parseInt) method of class (static), as the return type (int) , the required parameters of type String method.
1. StringBuffer append(boolean b)
2. StringBuffer append(char c)
3. StringBuffer append(char[] str)
4. StringBuffer append(char[] str, int offset, int len)
5. StringBuffer append(double d)
6. StringBuffer append(float f)
7. StringBuffer append(int i)
8. StringBuffer append(long l)
9. StringBuffer append(Object obj)
10. StringBuffer append(String str)
11. StringBuffer append(StringBuffer sb)

The above methods are "added" to the string buffer element, however, the "element" parameter may be a Boolean, character, character array, double, float, integer, long integer object type string, string StringBuffer and the like. If you add a character beyond the length of the string buffer, Java will automatically be expanded.

       String question = new String("1+1=");

            int answer = 3;

          boolean result = (1+1==3);

      

         StringBuffer sb = new StringBuffer();

            sb.append(question);

             sb.append(answer);

       sb.append('\t');

         sb.append(result);

      

         System.out.println(sb);

The results are:

1+1=3 false

12. int capacity() :
13. 返回当前StringBuffer对象(字符串缓冲区)的总空间,而非字符号串的长度。
14. 
15. char charAt(int index) :
16. 在当前StringBuffer对象中取索引号为index的字符。第一个字符的索引为“0”
17. 
18. StringBuffer delete(int start, int end) :
19. 删除当前StringBuffer对象中以索引号start开始,到end结束的子串。
20. 
21. StringBuffer deleteCharAt(int index) :
22. 删除当前StringBuffer对象中索引号为index的字符。
23. 
24. void ensureCapacity(int minimumCapacity) :
25. 重新设置字符号串缓冲区的总空间。如果minimumCapacity大于当前的总空间,则新的空间被设置:一种结果是minimumCapacity;另一种结果是{“老空间”乘2加2}。

                StringBuffer sb1 = new StringBuffer(5);

              StringBuffer sb2 = new StringBuffer(5);

             

             sb1.ensureCapacity(6);

               sb2.ensureCapacity(100);

            

             System.out.println( "sb1.Capacity: " + sb1.capacity() );

             System.out.println( "sb2.Capacity: " + sb2.capacity() );

The results are:

sb1.Capacity: 12

sb2.Capacity: 100

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) :
从当前StringBuffer对象的索引号srcBegin开始,到srcEnd结束的子串,
赋值到字符数组dst中,并且从dst的索引号dstBegin开始。

                 StringBuffer sb = new StringBuffer("I love her!");

           char[] i = {'I',' ','l','o','v','e',' ','y','o','u'};

               

             sb.getChars(7,10,i,7);

              

             System.out.println( "sb: " + sb );

Results: sb: I love her!

18. int indexOf(String str) :返回当前StringBuffer对象中,第一个满足str子串的位置。
19. int indexOf(String str, int fromIndex) :从当前StringBuffer对象的fromIndex开始查找,返回第一个满足str子串的位置。
20. StringBuffer insert(int offset, boolean b)
21. StringBuffer insert(int offset, char c)
22. StringBuffer insert(int offset, char[] str)
23. StringBuffer insert(int index, char[] str, int offset, int len)
24. StringBuffer insert(int offset, double d)
25. StringBuffer insert(int offset, float f)
26. StringBuffer insert(int offset, int i)
27. StringBuffer insert(int offset, long l)
28. StringBuffer insert(int offset, Object obj)
29. StringBuffer insert(int offset, String str)

The above methods are inserted into an element in the current StringBuffer object, insert the appropriate offset value in the index.

30. int lastIndexOf(String str) :
31. 返回当前StringBuffer对象中,最后一个满足str子串的位置。
32. 
33. int lastIndexOf(String str, int fromIndex) :
34. 从当前StringBuffer对象的fromIndex开始查找,返回最后一个满足str子串的位置。
35. 
36. int length() :
37. 返回当前StringBuffer对象(字符缓冲区)中,字符串的长度。注意:此方法与capacity() 不同。
38. 
39. StringBuffer replace(int start, int end, String str) :
40. 替换当前StringBuffer对象的字符串。从start开始,到end结束的位置替换成str。
41. 
42. StringBuffer reverse() :将字符串翻转。

               StringBuffer sb = new StringBuffer("0123456789");

            System.out.println( "sb.reverse(): " + sb.reverse() );

Results: sb.reverse (): 9876543210

35. void setCharAt(int index, char ch) :
36. 设置索引号index的字符为ch。
37. 
38. void setLength(int newLength) :
39. 重新设置字符串缓冲区中字符串的长度,如果newLength小于当前的字符串长度,将截去多余的字符。

              StringBuffer sb = new StringBuffer("0123456789");

            sb.setLength(5);

             System.out.println( "sb: " + sb );

Results: sb: 01234

37. String substring(int start) :
38. 取当前StringBuffer对象中,从start开始到结尾的子串。
39. 
40. String substring(int start, int end) :
41. 取当前StringBuffer对象中,从start开始到end的子串。
42. 
43. String toString() :
44. 将当前StringBuffer对象转换成String对象。
Released 1148 original articles · won praise 10000 + · views 420 000 +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104312138