Some common methods of Java StringBuffer class

Table of contents

1.append(String s)

2.delete(int start, int end)

3.insert()

4.replace(int start, int end, String str)

5.reverse()

6.capacity()

7.ensureCapacity(int minimumCapacity)

8.charAt(int index)

9.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

10.indexOf()

11.lastIndexOf()

12.length()

13.setCharAt(int index, char ch)

14.setLength(int newLength)

15.subSequence(int start, int end)

16.substring(int start)

17.substring(int start, int end)

18.toString()


1.append(String s)

Appends the specified string to this sequence of strings

2.delete(int start, int end)

Remove the characters in the substring of this sequence (closed before and opened after)

3.insert()

Including public insert(int offset, int i) and insert(int offset, String str), respectively inserting int parameters and str parameters into the sequence

4.replace(int start, int end, String str)

Replaces the characters in substrings of this sequence with the characters in the given String

5.reverse()

reverse this string column

public class Example {
    public static void main(String[] args) {
        StringBuffer sb=new StringBuffer();
        System.out.println(sb.append("江畔何人初见月?江月何年初照人?"));
        System.out.println(sb.delete(8,15));
        System.out.println(sb.insert(8,"江月何年初照人"));
        System.out.println(sb.replace(0,16,"人生代代无穷已,江月年年望相似。"));
        System.out.println(sb.reverse());
    }
}
江畔何人初见月?江月何年初照人?
江畔何人初见月??
江畔何人初见月?江月何年初照人?
人生代代无穷已,江月年年望相似。
。似相望年年月江,已穷无代代生人

6.capacity()

return current capacity

Note: When StringBuffer allocates a length for an object, it will initially allocate a word, that is, two bytes in length (16 bits). For each additional character in the future, the length will be increased by 1 on the basis of 16.

7.ensureCapacity(int minimumCapacity)

Make sure the capacity is at least equal to the specified minimum

If the minimumCapacity parameter is greater than twice the old capacity plus 2, the new capacity is equal to minimumCapacity, otherwise the new capacity is equal to twice the old capacity+2

public void ensureCapacity​(int minimumCapacity)
Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:
The argument. minimumCapacity
Twice the old capacity, plus . 2
If the argument is nonpositive, this method takes no action and simply returns. Note that subsequent operations on this object can reduce the actual capacity below that requested here.minimumCapacity
Parameters:
minimumCapacity - the minimum desired capacity.
public class Example1 {
    public static void main(String[] args) {
        StringBuffer sb=new StringBuffer("江畔何人初见月?江月何年初照人?");
        //StringBuffer在为对象分配长度的时候,
        //起始会分配一个字,也就是两个字节长度即(16位)
        System.out.println("Before:"+sb.capacity());
        sb.ensureCapacity(30);
        System.out.println("After:"+sb.capacity());
        sb.ensureCapacity(99);
        System.out.println("After:"+sb.capacity());
    }
}
Before:32
After:32
After:99

8.charAt(int index)

Returns the char value at the specified index in this sequence

9.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

copies characters from this sequence to the destination character array

public class Example1 {
    public static void main(String[] args) {
        StringBuffer sb=new StringBuffer("江畔何人初见月?江月何年初照人?");
        char[] ch=new char[10];
        System.out.println(sb.charAt(3));
        sb.getChars(0, 8, ch, 0);
        System.out.println(ch);
    }
}

江畔何人初见月?

10.indexOf()

Including indexOf(String str) and indexOf(String str, int fromIndex), the former returns the index of the first occurrence of the specified string in the string, and the latter starts from the specified index and returns the first occurrence The index within the string of the specified substring of

11.lastIndexOf()

Including lastIndexOf(String str) and lastIndexOf(String str, int fromIndex), the former returns the index of the specified substring that appears on the far right in this string, and the latter returns the last occurrence position of the substring in the String object

public class Example {
    public static void main(String[] args) {
        StringBuffer sb=new StringBuffer("江畔何人初见月?  江月何年初照人?");
        System.out.println(sb.indexOf("?"));
        System.out.println(sb.indexOf("?",10));
        System.out.println(sb.lastIndexOf("?"));
        System.out.println(sb.lastIndexOf("?",10));
        System.out.println(sb.lastIndexOf("?",20));
    }
}
7
17
17
7
17

12.length()

Returns the length of the string

13.setCharAt(int index, char ch)

set the character at the given index to ch

14.setLength(int newLength)

set the length of the character sequence

15.subSequence(int start, int end)

This method returns a new sequence of characters that is a subsequence of this sequence

16.substring(int start)

This method returns a new String containing the character subsequence currently contained in this character sequence

17.substring(int start, int end)

This method returns a new String containing a subsequence of characters currently contained in this sequence

18.toString()

This method returns a string representation of the data in this sequence

public class Example1 {
    public static void main(String[] args) {
        StringBuffer sb=new StringBuffer("江畔何人初见月?江月何年初照人?");
        System.out.println(sb.length());
        sb.setCharAt(7,',');  //注意用单引号,且不能直接System,因为返回值是void
        System.out.println(sb);
        sb.setLength(30);
        System.out.println(sb.length());
        System.out.println(sb.subSequence(0,7));
        System.out.println(sb.substring(8));
        System.out.println(sb.substring(0,7));
        System.out.println(sb.toString());
    }
}
16
江畔何人初见月,江月何年初照人?
30
江畔何人初见月
江月何年初照人?

Guess you like

Origin blog.csdn.net/senxu_/article/details/126211728