程序员社区

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) 打赏
未经允许不得转载:IDEA激活码 » jdk StringBuilder实现

一个分享Java & Python知识的社区