Java StringBuffer类的一些常用方法

目录

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)

将指定的字符串追加到此字符串序列之后

2.delete(int start, int end)

移除此序列的子字符串中的字符(前闭后开)

3.insert()

包括public insert(int offset, int i)和insert(int offset, String str),分别是把int参数和str参数插入序列

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

使用给定String中的字符替换此序列的子字符串中的字符

5.reverse()

将此字符串列反转

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()

返回当前容量

注意:StringBuffer在为对象分配长度的时候,起始会分配一个字,也就是两个字节长度即(16位),后续每增加一个字符,长度就在16的基础上加1

7.ensureCapacity(int minimumCapacity)

确保容量至少等于指定的最小值

如果minimumCapacity参数大于旧容量的两倍加2,则新容量等于minimumCapacity,否则新容量等于旧容量的两倍+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)

返回此序列中指定索引处的char值

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

将字符从此序列复制到目标字符数组

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()

包括 indexOf(String str)和indexOf(String str, int fromIndex),前者返回第一次出现的指定字符串在该字符串中的索引,后者则是从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引

11.lastIndexOf()

包括lastIndexOf(String str)和lastIndexOf(String str, int fromIndex),前者返回最右边出现的指定子字符串在此字符串中的索引,后者返回String对象中子字符串最后出现的位置

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()

返回字符串的长度

13.setCharAt(int index, char ch)

将给定索引处的字符设置为ch

14.setLength(int newLength)

设置字符序列的长度

15.subSequence(int start, int end)

此方法返回的一个新的字符序列,该字符序列是此序列的子序列

16.substring(int start)

此方法返回一个新的String,包含此字符序列当前所包含的字符子序列

17.substring(int start, int end)

此方法返回一个新的String,包含此序列当前所包含的字符子序列

18.toString()

此方法返回此序列中数据的字符串表现形式

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
江畔何人初见月
江月何年初照人?

猜你喜欢

转载自blog.csdn.net/senxu_/article/details/126211728