jdk StringBuilder实现

继承了AbstractStringBuilder类
实现了java.io.Serializable, CharSequence接口

一、常用用法

1.1 在指定位置插入字符串

使用insert函数

public class test {

    public static void main(String[] args) {
        StringBuilder sb=new StringBuilder("012345");
        sb.insert(2,"insert");
        System.out.println(sb.toString());
    }
}

输出:

01insert2345

1.2 删除某些字符

使用delete或者deleteCharAt函数

public class test {

    public static void main(String[] args) {
        String temp="0123456";
        StringBuilder sb=new StringBuilder(temp);
        sb.deleteCharAt(6);
        System.out.println(sb.toString());
        sb.delete(0,2);
        System.out.println(sb.toString());
    }

}

输出:

012345
2345

1.3 修改某个位置的字符

使用replace函数

public class test {

    public static void main(String[] args) {
        String temp="0123456";
        StringBuilder sb=new StringBuilder(temp);
        sb.replace(1,2,"test");
        System.out.println(sb.toString());
    }

}

输出:

0test23456

一、属性

二、方法

2.1 toString

@Override
    public String toString() {
        // Create a copy, don't share the array
        return new String(value, 0, count);
    }

2.2 delete

/**
     * @throws StringIndexOutOfBoundsException {@inheritDoc}
     */
    @Override
    public StringBuilder delete(int start, int end) {
        super.delete(start, end);
        return this;
    }

2.3 deleteCharAt

/**
     * @throws StringIndexOutOfBoundsException {@inheritDoc}
     */
    @Override
    public StringBuilder deleteCharAt(int index) {
        super.deleteCharAt(index);
        return this;
    }

2.4 append

@Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }

2.5 getChars

/**
     * Copies characters from this string into the destination character
     * array.
     * <p>
     * The first character to be copied is at index {@code srcBegin};
     * the last character to be copied is at index {@code srcEnd-1}
     * (thus the total number of characters to be copied is
     * {@code srcEnd-srcBegin}). The characters are copied into the
     * subarray of {@code dst} starting at index {@code dstBegin}
     * and ending at index:
     * <blockquote><pre>
     *     dstBegin + (srcEnd-srcBegin) - 1
     * </pre></blockquote>
     *
     * @param      srcBegin   index of the first character in the string
     *                        to copy.
     * @param      srcEnd     index after the last character in the string
     *                        to copy.
     * @param      dst        the destination array.
     * @param      dstBegin   the start offset in the destination array.
     * @exception IndexOutOfBoundsException If any of the following
     *            is true:
     *            <ul><li>{@code srcBegin} is negative.
     *            <li>{@code srcBegin} is greater than {@code srcEnd}
     *            <li>{@code srcEnd} is greater than the length of this
     *                string
     *            <li>{@code dstBegin} is negative
     *            <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than
     *                {@code dst.length}</ul>
     */
    public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
        if (srcBegin < 0) {
            throw new StringIndexOutOfBoundsException(srcBegin);
        }
        if (srcEnd > value.length) {
            throw new StringIndexOutOfBoundsException(srcEnd);
        }
        if (srcBegin > srcEnd) {
            throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
        }
        System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
    }

2.6 insert

在指定偏移量后插入一个String字符串

/**
     * @throws StringIndexOutOfBoundsException {@inheritDoc}
     */
    @Override
    public StringBuilder insert(int offset, String str) {
        super.insert(offset, str);
        return this;
    }

在指定偏移量后面插入一个char字符

/**
     * @throws StringIndexOutOfBoundsException {@inheritDoc}
     */
    @Override
    public StringBuilder insert(int offset, char[] str) {
        super.insert(offset, str);
        return this;
    }

2.7 replace

/**
     * @throws StringIndexOutOfBoundsException {@inheritDoc}
     */
    @Override
    public StringBuilder replace(int start, int end, String str) {
        super.replace(start, end, str);
        return this;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JDK8 中,Java引入了一种新的字符串拼接方法,即使用 `StringBuilder` 类来进行字符串拼接。这种方法比传统的字符串拼接方式(使用 `+` 运算符)更加高效。 传统的字符串拼接方式,每次拼接字符串时都会创建一个新的字符串对象,造成频繁的内存分配和回收,影响程序的性能。而使用 `StringBuilder` 类的方式,可以通过在一个可变的字符串缓冲区中进行字符串拼接,避免了频繁地创建新的字符串对象,提高了程序的性能。 例如,我们可以使用 `StringBuilder` 类来拼接两个字符串: ```java String str1 = "hello"; String str2 = "world"; StringBuilder sb = new StringBuilder(); sb.append(str1).append(" ").append(str2); String result = sb.toString(); ``` 在上面的代码中,我们首先创建了两个字符串 `str1` 和 `str2`。然后,我们创建了一个 `StringBuilder` 对象 `sb`,并使用 `append()` 方法将两个字符串拼接在一起,最后通过 `toString()` 方法将 `StringBuilder` 对象转换为字符串。 需要注意的是,在 JDK8 中,字符串拼接也可以使用新的语法糖,即使用 `+` 运算符来拼接字符串,例如: ```java String str1 = "hello"; String str2 = "world"; String result = str1 + " " + str2; ``` 虽然这种方式看起来更简洁,但实际上它仍然会创建多个临时的字符串对象,因此在需要频繁进行字符串拼接的情况下,建议使用 `StringBuilder` 类的方式,以提高程序的性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值