Module  java.base
Software package  java.lang

Class StringBuilder

  • All implemented interfaces
    Serializable , Appendable , CharSequence , Comparable<StringBuilder>

    public final class StringBuilder
    extends Object
    implements Serializable, Comparable<StringBuilder>, CharSequence
    A mutable sequence of characters. This class provides an API compatible withStringBufferCompatible API, but synchronization is not guaranteed. This class is designed to be used asStringBufferA replacement for places where a string buffer is used by a single thread (the usual case). Where possible, it is recommended to use this class in preference toStringBufferbecause it will be faster in most implementations.

    StringBuilderThe main operation on it isappendandinsertmethods, which are overloaded to accept any type of data. Each effectively converts the given data to a string, then appends or inserts the characters of that string into the string builder. appendmethod always appends these characters at the end of the builder; insertThe method adds characters at the specified point.

    For example, ifzrefers to its current contents as “startthe string builder object, then the method callz.append("le")will cause the string builder to contain “startle”, andz.insert(4, "le")Will change the string builder to containstarlet ”。

    In general, if one is referring to an instanceStringBuilder, thensb.append(x)has the same effectsb.insert(sb.length(), x) 。

    Each string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it automatically becomes larger.

    StringBuilderInstances are not suitable for multi-threaded use. If such synchronization is required, it is recommended to useStringBuffer 。

    Unless otherwise specified, it willnullparameter passed tonullthe constructor or method in ... will cause ... to be thrown.NullPointerException 。

    API Note:
    StringBuilderImplementationComparablebut does not overrideequals 。 Therefore,StringBuilderThe natural ordering is inconsistent with equals. ifStringBuilderobject used asStringBuilderkey in orSortedMapelements,SortedSet 。 seeComparable , SortedMap, orSortedSetFor more information.
    Starting from the following version:
    1.5
    See also:
    StringBuffer , String , Serialized Form
    • Constructor Summary

      Constructor  
      Constructor Description
      StringBuilder()
      Constructs a string builder with no characters and an initial capacity of 16 characters.
      StringBuilder​(int capacity)
      Constructs a string builder with no characters in it and withcapacityThe initial capacity specified by the parameter.
      StringBuilder​(CharSequence seq)
      Constructs a string builder that contains the same ... as the specifiedCharSequencethe same character.
      StringBuilder​(String str)
      Constructs a string builder initialized to the contents of the specified string.
    • Method Summary

      All methods  Instance Methods Specific Methods 
      Variables and types Methods Description
      StringBuilder append​(boolean b)
      willbooleanAppends the string representation of the argument to the sequence.
      StringBuilder append​(char c)
      willcharAppends the string representation of the argument to this sequence.
      StringBuilder append​(char[] str)
      willcharThe string representation of the array argument is appended to this sequence.
      StringBuilder append​(char[] str, int offset, int len)
      willcharAppends the string representation of a subarray of the array argument to this sequence.
      StringBuilder append​(double d)
      willdoubleAppends the string representation of the argument to this sequence.
      StringBuilder append​(float f)
      willfloatAppends the string representation of the argument to this sequence.
      StringBuilder append​(int i)
      willintAppends the string representation of the argument to this sequence.
      StringBuilder append​(long lng)
      willlongAppends the string representation of the argument to this sequence.
      StringBuilder append​(CharSequence s)
      Appends the specified character sequence to thisAppendable 。
      StringBuilder append​(CharSequence s, int start, int end)
      the specifiedCharSequenceThe sequence is appended to this sequence.
      StringBuilder append​(Object obj)
      AppendObjectThe string representation of the argument.
      StringBuilder append​(String str)
      Appends the specified string to this character sequence.
      StringBuilder append​(StringBuffer sb)
      the specifiedStringBufferAppends to this sequence.
      StringBuilder appendCodePoint​(int codePoint)
      willcodePointAppends the string representation of the argument to this sequence.
      int capacity()
      Returns the current capacity.
      char charAt​(int index)
      Returns the character at the specified index in this sequencecharvalue.
      IntStream chars()
      returninta stream of the elements in this sequencecharThe value is zero-extended.
      int codePointAt​(int index)
      Returns the character (Unicode code point) at the specified index.
      int codePointBefore​(int index)
      Returns the character (Unicode code point) before the specified index.
      int codePointCount​(int beginIndex, int endIndex)
      Returns the number of Unicode code points in the specified text range of this sequence.
      IntStream codePoints()
      Returns a stream of code point values from this sequence.
      int compareTo​(StringBuilder another)
      StringBuilderCompares two sequences lexicographicallyStringBuilderinstance.
      StringBuilder delete​(int start, int end)
      Deletes the characters in a substring of this sequence.
      StringBuilder deleteCharAt​(int index)
      Removes from this list the element at the specified positionchar 。
      void ensureCapacity​(int minimumCapacity)
      Ensures that the capacity is at least equal to the specified minimum.
      void getChars​(int srcBegin, int srcEnd, char[] dst, int dstBegin)
      Copies characters from this sequence to the destination character array.dst 。
      int indexOf​(String str)
      Returns the index within the string of the first occurrence of the specified substring.
      int indexOf​(String str, int fromIndex)
      Returns the index within the string of the first occurrence of the specified substring, starting at the specified index.
      StringBuilder insert​(int offset, boolean b)
      willbooleanInserts the string representation of the argument into this sequence.
      StringBuilder insert​(int offset, char c)
      willcharInserts the string representation of the argument into this sequence.
      StringBuilder insert​(int offset, char[] str)
      willcharThe string representation of the array argument is inserted into this sequence.
      StringBuilder insert​(int index, char[] str, int offset, int len)
      willstrInserts the string representation of a subarray of the array argument into this sequence.
      StringBuilder insert​(int offset, double d)
      willdoubleInserts the string representation of the argument into this sequence.
      StringBuilder insert​(int offset, float f)
      willfloatInserts the string representation of the argument into this sequence.
      StringBuilder insert​(int offset, int i)
      the secondintInserts the string representation of the argument into this sequence.
      StringBuilder insert​(int offset, long l)
      willlongInserts the string representation of the argument into this sequence.
      StringBuilder insert​(int dstOffset, CharSequence s)
      the specifiedCharSequenceinserted into this sequence.
      StringBuilder insert​(int dstOffset, CharSequence s, int start, int end)
      the specifiedCharSequenceInserts the sequence into this sequence.
      StringBuilder insert​(int offset, Object obj)
      willObjectThe string representation of the argument is inserted into this character sequence.
      StringBuilder insert​(int offset, String str)
      Inserts the string into this character sequence.
      int lastIndexOf​(String str)
      Returns the index within the string of the last occurrence of the specified substring.
      int lastIndexOf​(String str, int fromIndex)
      Returns the index within the string of the last occurrence of the specified substring, searching backward from the specified index.
      int length()
      Returns the length (number of characters).
      int offsetByCodePoints​(int index, int codePointOffset)
      Returns the index in this sequence that starts from the givenindexOffsetcodePointOffsetcode point.
      StringBuilder replace​(int start, int end, String str)
      using the specifiedStringcharacters replace the characters in a substring of this sequence.
      StringBuilder reverse()
      This character sequence is caused by the reverse replacement of the sequence.
      void setCharAt​(int index, char ch)
      The character at the specified index is set toch 。
      void setLength​(int newLength)
      Sets the length of the character sequence.
      CharSequence subSequence​(int start, int end)
      Returns a new character sequence that is a subsequence of this sequence.
      String substring​(int start)
      returns a newString, which contains a subsequence of the characters currently contained in this character sequence.
      String substring​(int start, int end)
      returns a newString, containing a subsequence of the characters currently contained in this sequence.
      void trimToSize()
      Attempt to reduce the storage space used for the character sequence.
    • Constructor Detail

      • StringBuilder

        public StringBuilder()
        Constructs a string builder with no characters and an initial capacity of 16 characters.
      • StringBuilder

        public StringBuilder​(int capacity)
        Constructs a string builder that contains no characters andcapacityThe initial capacity specified by the parameter.
        Parameter
        capacity- The initial capacity.
        Exception
        NegativeArraySizeException- ifcapacitythe argument is less than0 。
      • StringBuilder

        public StringBuilder​(String str)
        Constructs a string builder initialized to the contents of the specified string. The initial capacity of the string builder is16plus the length of the string argument.
        Parameter
        str- The initial contents of the buffer.
      • StringBuilder

        public StringBuilder​(CharSequence seq)
        Constructs a string builder that contains the same ... as the specifiedCharSequencethe same character. The initial capacity of the string builder is16addCharSequencethe length of the argument.
        Parameter
        seq- The sequence to be copied.
    • Method Detail

      • compareTo

        public int compareTo​(StringBuilder another)
        StringBuilderCompares two sequences lexicographicallyStringBuilderinstance. This method followsCharSequence.compare(this, another)The same rules of dictionary comparison as defined in the method.

        For fine-grained, locale-sensitive string comparison, seeCollator 。

        Specified by:
        compareToIn the interfaceComparable<StringBuilder>
        Parameter
        another- the object to be compared with.StringBuilder
        Result
        Value0if thisStringBuildercontains the same characters as the parameterStringBuilderthe same character sequence; if thisStringBuilderlexicographically less thanStringBuilderparameters,StringBuildera negative integer; if thisStringBuilderlexicographically greater thanStringBuilderparameters,StringBuildera positive integer.
        Starting from the following version:
        11
      • append

        public StringBuilder append​(Object obj)
        AppendObjectThe string representation of the argument.

        The overall effect with the methodString.valueOf(Object)Converts the argument to a stringexactly the same, and then the characters of the string areappended 。

        Parameter
        obj - Object 。
        Result
        A reference to this object.
      • append

        public StringBuilder append​(String str)
        Appends the specified string to this character sequence.

        Appends in orderStringThe characters of the argument are appended, increasing the length of this sequence by the length of the argument. ifstrYesnull, then append four characters"null" 。

        designnis performedappendthe length of this character sequence before the method. ifkless thann, then the index in the new character sequencekthe character at ... is equal to the character at index ... in the old character sequence.kcharacter at; Otherwise, it is equal to the parameter.strindex inkncharacter at.

        Parameter
        str- a string.
        Result
        A reference to this object.
      • append

        public StringBuilder append​(StringBuffer sb)
        the specifiedStringBufferAppends to this sequence.

        StringBufferThe characters of the argument are appended to this sequence in order, increasing the length of this sequence by the length of the argument. ifsbYesnull, then the four characters"null"appended to this sequence.

        designnis performedappendthe length of this character sequence before the method. ifkless thann, then the index in the new character sequencekthe character at ... is equal to the character at index ... in the old character sequence.kcharacter at; Otherwise, it is equal to the parameter.sbindex inkncharacter at.

        Parameter
        sb- To be appended.StringBuffer 。
        Result
        A reference to this object.
      • append

        public StringBuilder append​(CharSequence s)
        Instructions copied from the interface:Appendable
        Appends the specified character sequence to thisAppendable 。

        according to which class implements the character sequencecsq, may not append the entire sequence. For example, ifcsqYesCharBuffer,the subsequence to be appended is defined by the buffer's position and limit.

        Specified by:
        appendIn the interfaceAppendable
        Parameter
        s- the character sequence to be appended. ifcsqYesnull, then the four characters"null"Will be appended to this Appendable.
        Result
        ReferenceAppendable
      • append

        public StringBuilder append​(CharSequence s,
                                    int start,
                                    int end)
        the specifiedCharSequenceThe sequence is appended to this sequence.

        from indexstartthe start parametersCharacters are appended to the contents of this sequence in order, up to the index (exclusive)end 。 The length of the sequence is increased by the value.end - start 。

        designnis performedappendthe length of this character sequence before the method. then, ifkless thann, then the index in the character sequenceAt kthe character becomes equal to the index in this sequencekcharacter at; Otherwise, it is equal to the parameter.sindex ink + start-nAt thes 。

        ifsYesnull, then this method appends characters as if the s parameter were a string containing four characters"null"sequence of"null" 。

        Specified by:
        appendIn the interfaceAppendable
        Parameter
        s- the sequence to be appended.
        startThe starting index of the subsequence to append.
        endThe ending index of the subsequence to append.
        Result
        A reference to this object.
        Exception
        IndexOutOfBoundsException- ifstartis negative, orstartgreater thanendorendgreater thans.length()
      • append

        public StringBuilder append​(char[] str)
        willcharThe string representation of the array argument is appended to this sequence.

        The characters of the array argument are appended, in order, to the contents of this sequence. The length of this sequence increases by the length of the argument.

        The overall effect is just as if through the methodString.valueOf(char[])Converts the argument to a stringSame, and the characters of that string are then those of this character sequenceappended 。

        Parameter
        str- the character to be appended.
        Result
        A reference to this object.
      • append

        public StringBuilder append​(char[] str,
                                    int offset,
                                    int len)
        willcharAppends the string representation of a subarray of the array argument to this sequence.

        from indexoffsetstartingcharArraystrThe characters are appended to the contents of this sequence in order. The value of the length increase of this sequencelen 。

        The overall effect is just as if through the methodString.valueOf(char[],int,int)Converts the argument to a stringSame, and the characters of the string for this character sequenceThen isappended 。

        Parameter
        str- the character to be appended.
        offset- the first character to be appended.charthe index of.
        len- To be appended.charthe number of.
        Result
        A reference to this object.
        Exception
        IndexOutOfBoundsException- if it isoffset < 0orlen < 0oroffset+len > str.length
      • append

        public StringBuilder append​(boolean b)
        willbooleanAppends the string representation of the argument to the sequence.

        The overall effect is just as if through the methodString.valueOf(boolean)Converts the argument to a stringSame, and the characters of that string are then those of this character sequenceappended 。

        Parameter
        b - boolean 。
        Result
        A reference to this object.
      • append

        public StringBuilder append​(char c)
        willcharAppends the string representation of the argument to this sequence.

        The argument is appended to the contents of this sequence. The length of this sequence increases1 。

        The overall effect is just as if through the methodString.valueOf(char)Converts the argument to a stringSameand the characters in the string are subsequently those of the character sequenceappended 。

        Specified by:
        appendIn the interfaceAppendable
        Parameter
        c - a char 。
        Result
        A reference to this object.
      • append

        public StringBuilder append​(int i)
        willintAppends the string representation of the argument to this sequence.

        The overall effect with the methodString.valueOf(int)Converts the argument to a stringexactly the same, and then the characters of the string areappended 。

        Parameter
        i - int 。
        Result
        A reference to this object.
      • append

        public StringBuilder append​(long lng)
        willlongAppends the string representation of the argument to this sequence.

        The overall effect with the methodString.valueOf(long)Converts the argument to a stringexactly the same, and then the characters of the string areappended 。

        Parameter
        lng - long 。
        Result
        A reference to this object.
      • append

        public StringBuilder append​(float f)
        willfloatAppends the string representation of the argument to this sequence.

        The overall effect is just as if through the methodString.valueOf(float)Converts the argument to a stringSame, and the characters of that string are then those of this character sequenceappended 。

        Parameter
        f - a float 。
        Result
        A reference to this object.
      • append

        public StringBuilder append​(double d)
        willdoubleAppends the string representation of the argument to this sequence.

        The overall effect is just as if through the methodString.valueOf(double)Converts the argument to a stringSame, and the characters of that string are then those of this character sequenceappended 。

        Parameter
        d - a double 。
        Result
        A reference to this object.
      • appendCodePoint

        public StringBuilder appendCodePoint​(int codePoint)
        willcodePointAppends the string representation of the argument to this sequence.

        The argument is appended to the contents of this sequence. The length of this sequence increasesCharacter.charCount(codePoint) 。

        The overall effect is just as if through the methodCharacter.toChars(int)converts the argument tocharsame as the array, and the characters in the arrayThen isappended 。

        Parameter
        codePoint- Unicode code point
        Result
        A reference to this object.
        Starting from the following version:
        1.5
      • delete

        public StringBuilder delete​(int start,
                                    int end)
        Deletes the characters in a substring of this sequence. Substring from the specifiedstartbegins, and if no such character exists, then extends to the indexend - 1the character at, or the end of, the sequence. ifstartequalsend, then no changes are made.
        Parameter
        start- the starting index, inclusive.
        end- the ending index, exclusive.
        Result
        this object.
        Exception
        StringIndexOutOfBoundsException- ifstartis negative, greater thanlength(), or greater thanend 。
      • deleteCharAt

        public StringBuilder deleteCharAt​(int index)
        Removes from this list the element at the specified positionchar 。 the sequence consists of acharshortened.

        Note: If the character at the given index is a supplementary character, this method does not delete the entire character. If you need to correctly handle supplementary characters, callCharacter.charCount(thisSequence.codePointAt(index))determines what is to be deletedcharthe number of s, wherethisSequenceis this sequence.

        Parameter
        index- the index number to be deleted.char
        Result
        this object.
        Exception
        StringIndexOutOfBoundsException- ifindexis negative or greater than or equal to.length() 。
      • replace

        public StringBuilder replace​(int start,
                                     int end,
                                     String str)
        using the specifiedStringcharacters replace the characters in a substring of this sequence. Substring from the specifiedstartstarts, if no such character exists, extends to the indexend - 1the character at, or the end of, the sequence. First remove the characters in the substring, then atStringInserts the specifiedstart 。 (If necessary, this sequence will be extended to accommodate the specified string.)
        Parameter
        start- the starting index, inclusive.
        end- the ending index, exclusive.
        str- the string that replaces the previous contents.
        Result
        this object.
        Exception
        StringIndexOutOfBoundsException- ifstartis negative, greater thanlength(), or greater thanend 。
      • insert

        public StringBuilder insert​(int index,
                                    char[] str,
                                    int offset,
                                    int len)
        willstrInserts the string representation of a subarray of the array argument into this sequence. subarray from the specifiedoffsetstarts, and extendslen char s。 the characters of the subarray are inindexinsert into this sequence at the indicated position. The length of this sequence increaseslen char s。
        Parameter
        index- the position at which to insert the subarray.
        str - chararray.
        offset- the first character in the subarray to be inserted.charthe index of.
        len- in the subarray to be inserted.charthe number of s.
        Result
        this object
        Exception
        StringIndexOutOfBoundsException- ifindexis negative or greater thanlength(), oroffsetorlenis negative, or(offset+len)greater thanstr.length 。
      • insert

        public StringBuilder insert​(int offset,
                                    Object obj)
        willObjectThe string representation of the argument is inserted into this character sequence.

        the overall effect is the same as if the second argument were passed through the method.String.valueOf(Object)converted to a stringexactly the same, and the character of this string in the character sequence at the specified offset isinserted 。

        offsetThe argument must be greater than or equal to0, and is less than or equal to this sequence'slength 。

        Parameter
        offset- The offset.
        obj - Object 。
        Result
        A reference to this object.
        Exception
        StringIndexOutOfBoundsException- if the offset is invalid.
      • insert

        public StringBuilder insert​(int offset,
                                    String str)
        Inserts the string into this character sequence.

        StringThe characters of the argument are inserted into this sequence at the specified offset in order, shifting any characters originally above that position upward, and increasing the length of this sequence by the length of the argument. ifstrYesnull, then the four characters"null"Inserted into this sequence.

        Index in the new character sequencekthe character at the position is equal to:

        • ifkless thanoffset, then the index in the old character sequencekthe character at
        • Parameterstrthe index withink -offsetthe characters, ifknot less thanoffsetbut less thanoffset+str.length()
        • the index in the old character sequence isk -str.length()the characters, ifknot less thanoffset+str.length()

        offsetThe argument must be greater than or equal to0, and is less than or equal to this sequence'slength 。

        Parameter
        offset- The offset.
        str- a string.
        Result
        A reference to this object.
        Exception
        StringIndexOutOfBoundsException- if the offset is invalid.
      • insert

        public StringBuilder insert​(int offset,
                                    char[] str)
        willcharThe string representation of the array argument is inserted into this sequence.

        The characters of the array argument atoffsetat the indicated position into the contents of this sequence. The length of this sequence increases by the length of the argument.

        The overall effect is as if the second argument were passed through the methodString.valueOf(char[])converted to a stringSameand the characters of this string at the specified offset are those of the character sequenceinserted 。

        offsetThe argument must be greater than or equal to0, and is less than or equal to this sequence'slength 。

        Parameter
        offset- The offset.
        str- The character array.
        Result
        A reference to this object.
        Exception
        StringIndexOutOfBoundsException- if the offset is invalid.
      • insert

        public StringBuilder insert​(int dstOffset,
                                    CharSequence s)
        the specifiedCharSequenceinserted into this sequence.

        CharSequenceInserts the characters of the parameter into this sequence at the specified offset, shifting up any characters initially above that position, and increasing the length of this sequence by the length of parameter s.

        The result of this method is the same as calling this object'sinsertthe (dstOffset, s, 0, s.length()) methodexactly the same 。

        ifsYesnull, then the four characters"null"Inserts into this sequence.

        Parameter
        dstOffset- The offset.
        s- the sequence to be inserted
        Result
        A reference to this object.
        Exception
        IndexOutOfBoundsException- if the offset is invalid.
      • insert

        public StringBuilder insert​(int dstOffset,
                                    CharSequence s,
                                    int start,
                                    int end)
        the specifiedCharSequenceInserts the sequence into this sequence.

        subsequence of the argumentsby specifyingstartandendare inserted at the specified destination offset in this sequence, moving up any characters originally above that position. the length of this sequence increases byend - start 。

        index in this sequencekthe character at the position becomes equal to:

        • ifkless thandstOffset, then the index in this sequencekthe character at
        • Parametersthe index withink +start-dstOffsetthe characters, ifkgreater than or equal todstOffsetbut less thandstOffset+end-start
        • ifkgreater than or equal todstOffset+end-start, then the index in this sequence isk -(end-start)characters of

        dstOffsetThe argument must be greater than or equal to0, and less than or equal to this sequence'slength 。

        The start parameter must be non-negative and not greater thanend 。

        the end parameter must be greater than or equal tostart, and less than or equal to the length of s.

        ifsYesnull, then this method inserts characters as if the s parameter were a string containing four characters"null"sequence of"null" 。

        Parameter
        dstOffset- the offset in this sequence.
        s- the sequence to be inserted.
        startThe starting index of the subsequence to insert.
        endThe ending index of the subsequence to insert.
        Result
        A reference to this object.
        Exception
        IndexOutOfBoundsException- ifdstOffsetis negative or greater thanthis.length(), orstartorendis negative, orstartgreater thanendorendgreater thans.length()
      • insert

        public StringBuilder insert​(int offset,
                                    boolean b)
        willbooleanInserts the string representation of the argument into this sequence.

        the overall effect is the same as if the second argument were passed through the method.String.valueOf(boolean)converted to a stringexactly the same, and the characters of that string are inserted into this character sequence at the specified offset, followed byinserted 。

        offsetThe argument must be greater than or equal to0, and is less than or equal to this sequence'slength 。

        Parameter
        offset- The offset.
        b - a boolean 。
        Result
        A reference to this object.
        Exception
        StringIndexOutOfBoundsException- if the offset is invalid.
      • insert

        public StringBuilder insert​(int offset,
                                    char c)
        willcharInserts the string representation of the argument into this sequence.

        The overall effect is the same as through the methodString.valueOf(char)Converts the second argument to a stringexactly the same, and the characters of that string are inserted into that character sequence at the specified offset, followed byinserted 。

        offsetThe argument must be greater than or equal to0, and is less than or equal to this sequence'slength 。

        Parameter
        offset- The offset.
        c - a char 。
        Result
        A reference to this object.
        Exception
        IndexOutOfBoundsException- if the offset is invalid.
      • insert

        public StringBuilder insert​(int offset,
                                    int i)
        the secondintInserts the string representation of the argument into this sequence.

        The overall effect is as if the second argument were passed through the methodString.valueOf(int)converted to a stringSameand the characters of this string are in the character sequence at the specified offsetinserted 。

        offsetThe argument must be greater than or equal to0, and less than or equal to this sequence'slength 。

        Parameter
        offset- The offset.
        i - int 。
        Result
        A reference to this object.
        Exception
        StringIndexOutOfBoundsException- if the offset is invalid.
      • insert

        public StringBuilder insert​(int offset,
                                    long l)
        willlongInserts the string representation of the argument into this sequence.

        The overall effect is exactly the same as if the second argument were converted to a string by the methodString.valueOf(long), and the characters of the string theninsertedThe character sequence is formed at the indicated offset.

        offsetThe argument must be greater than or equal to0, and less than or equal to this sequence'slength 。

        Parameter
        offset- The offset.
        l - long 。
        Result
        A reference to this object.
        Exception
        StringIndexOutOfBoundsException- if the offset is invalid.
      • insert

        public StringBuilder insert​(int offset,
                                    float f)
        willfloatInserts the string representation of the argument into this sequence.

        The overall effect is the same as through the methodString.valueOf(float)Converts the second argument to a stringexactly the same, and the character of this string in the character sequence at the specified offset isinserted 。

        offsetThe argument must be greater than or equal to0, and is less than or equal to this sequence'slength 。

        Parameter
        offset- The offset.
        f - float 。
        Result
        A reference to this object.
        Exception
        StringIndexOutOfBoundsException- if the offset is invalid.
      • insert

        public StringBuilder insert​(int offset,
                                    double d)
        willdoubleInserts the string representation of the argument into this sequence.

        The overall effect is the same as through the methodString.valueOf(double)Converts the second argument to a stringexactly the same, and the character of this string in the character sequence at the specified offset isinserted 。

        offsetThe argument must be greater than or equal to0, and less than or equal to this sequence'slength 。

        Parameter
        offset- The offset.
        d - a double 。
        Result
        A reference to this object.
        Exception
        StringIndexOutOfBoundsException- if the offset is invalid.
      • indexOf

        public int indexOf​(String str)
        Returns the index within the string of the first occurrence of the specified substring.

        The returned index is the minimum valuek, where:

           this.toString().startsWith(str, k)  
        if there is no such valuekexists, then-1returns.
        Parameter
        str- The substring to search for.
        Result
        The index of the first occurrence of the specified substring, or -1 if there is no such occurrence.-1 。
      • indexOf

        public int indexOf​(String str,
                           int fromIndex)
        Returns the index within the string of the first occurrence of the specified substring, starting at the specified index.

        The returned index is the minimum valuek, where:

           k >= Math.min(fromIndex, this.length()) && this.toString().startsWith(str, k)  
        if there is no such valuekexists, then-1returns.
        Parameter
        str- The substring to search for.
        fromIndex- The index from which to start searching.
        Result
        The index of the first occurrence of the specified substring beginning at the specified index, or -1 if there is no such occurrence.-1 。
      • lastIndexOf

        public int lastIndexOf​(String str)
        Returns the index within the string of the last occurrence of the specified substring. The last occurrence of the empty string "" is considered to occur at the index valuethis.length() 。

        The returned index is the maximum valuek, where:

           this.toString().startsWith(str, k)  
        if there is no such valuekexists, then-1returns.
        Parameter
        str- The substring to search for.
        Result
        The index of the last occurrence of the specified substring, or -1 if there is no such occurrence.-1 。
      • lastIndexOf

        public int lastIndexOf​(String str,
                               int fromIndex)
        Returns the index within the string of the last occurrence of the specified substring, searching backward from the specified index.

        The returned index is the maximum valuek, where:

           k <= Math.min(fromIndex, this.length()) && this.toString().startsWith(str, k)  
        if there is no such valuekexists, then-1returns.
        Parameter
        str- The substring to search for.
        fromIndex- The index from which to start searching.
        Result
        the index of the last occurrence of the specified substring, searching backward from the specified index, if there is no such occurrence,-1 。
      • reverse

        public StringBuilder reverse()
        This character sequence is caused by the reverse replacement of the sequence. If the sequence contains any surrogate pairs, these surrogate pairs are treated as a single character for the reverse operation. Therefore, the order of high and low surrogates will never be reversed. designnis performedreversethe character length of this character sequence before the method (notcharthe length in the value). Then, the index in the new character sequencekthe character at ... is equal to the character at index ... in the old character sequence.nk-1character at.

        Note that the reverse operation may result in producing surrogate pairs that were unpaired low and high surrogates before the operation. For example, reversing “\ uDC00 \ uD800” generates “\ uD800 \ uDC00”, which is a valid surrogate pair.

        Result
        A reference to this object.
      • length

        public int length()
        Returns the length (number of characters).
        Specified by:
        lengthIn the interfaceCharSequence
        Result
        The length of the character sequence currently represented by this object.
      • capacity

        public int capacity()
        Returns the current capacity. Capacity is the amount of storage available for newly inserted characters, beyond which allocation will occur.
        Result
        current capacity
      • ensureCapacity

        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, a new internal array with a larger capacity is allocated. The new capacity is the larger of:
        • minimumCapacityparameters.
        • twice the old capacity, plus2 。
        ifminimumCapacityIf the parameter is non-positive, this method does nothing and simply returns. Note that subsequent operations on this object can reduce the actual capacity below the capacity requested here.
        Parameter
        minimumCapacity- the minimum capacity required.
      • trimToSize

        public void trimToSize()
        Attempt to reduce the storage space used for the character sequence. If the buffer is larger than necessary to hold its current sequence of characters, then it may be resized to be more space efficient. Calling this method may (but is not required to) affect subsequent calls.capacity()the value returned by the method.
      • setLength

        public void setLength​(int newLength)
        Sets the length of the character sequence. The sequence is changed to a new character sequence whose length is specified by the argument. for every non-negative index less thank newLength, at the index of the new character sequencekThe character at the position is the same at the old sequence index.kifkcharacters less than the length of the original character sequence; Otherwise, it is the null character'\u0000' 。 In other words, ifnewLengthIf the argument is less than the current length, then the length will be changed to the specified length.

        ifnewLengthIf the parameter is greater than or equal to the current length, then sufficient null characters ('\u0000'), so that length becomesnewLengthparameters.

        newLengthThe argument must be greater than or equal to0 。

        Parameter
        newLength- new length
        Exception
        IndexOutOfBoundsException- ifnewLengthThe parameter is negative.
      • charAt

        public char charAt​(int index)
        Returns the character at the specified index in this sequencecharvalue. firstcharvalue at index0, the next is at index1, and so on, as shown in the array indices.

        The index parameter must be greater than or equal to0, and less than the length of this sequence.

        if the index specifiedcharValue issurrogate, then a surrogate value is returned.

        Specified by:
        charAtIn the interfaceCharSequence
        Parameter
        index- requiredcharthe index of the value.
        Result
        at the specified indexcharvalue.
        Exception
        IndexOutOfBoundsException- ifindexis negative or greater than or equal to.length() 。
      • codePointAt

        public int codePointAt​(int index)
        Returns the character (Unicode code point) at the specified index. index referencecharvalue (Unicode code units), ranging from0toCharSequence.length() - 1 。

        If the specified at the given indexcharIf the value is in the high surrogate range, then the following index is less than the length of this sequence, and the value at the following indexcharIf the value is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, return the one at the given indexcharvalue.

        Parameter
        index - charthe index of the value
        Result
        indexthe code point value of the character at the specified index.
        Exception
        IndexOutOfBoundsException- ifindexThe argument is negative or not less than the length of this sequence.
      • codePointBefore

        public int codePointBefore​(int index)
        Returns the character (Unicode code point) before the specified index. index referencecharvalue (Unicode code units), ranging from1toCharSequence.length() 。

        ifcharAt value(index - 1)is in the low surrogate range,(index - 2)is not negative, andcharAt value(index - 2)is in the high surrogate range, then the supplementary code point value of the surrogate pair is returned. ifcharAt valueindex - 1If it is an unpaired low surrogate or a high surrogate, then the surrogate value is returned.

        Parameter
        index- the index following the code point to be returned.
        Result
        The Unicode code point value before the given index.
        Exception
        IndexOutOfBoundsException- ifindexThe argument is less than 1 or greater than the length of this sequence.
      • codePointCount

        public int codePointCount​(int beginIndex,
                                  int endIndex)
        Returns the number of Unicode code points in the specified text range of this sequence. The text range starts at the specifiedbeginIndex, and extends tocharat indexendIndex - 1 。 Therefore, the length of the text range (incharin s) isendIndex-beginIndex 。 Unpaired surrogates in this sequence count as one code point each.
        Parameter
        beginIndex- the first character of the text range.charthe index of.
        endIndex- the last character of the text range.charthe index after.
        Result
        The number of Unicode code points in the specified text range.
        Exception
        IndexOutOfBoundsException- ifbeginIndexis negative, orendIndexgreater than the length of this sequence, orbeginIndexgreater thanendIndex 。
      • offsetByCodePoints

        public int offsetByCodePoints​(int index,
                                      int codePointOffset)
        Returns the index in this sequence that starts from the givenindexOffsetcodePointOffsetcode point. indexandcodePointOffsetUnpaired surrogates in the given text range count as one code point each.
        Parameter
        index- the index to be offset.
        codePointOffset- the offset in code points.
        Result
        index in this sequence
        Exception
        IndexOutOfBoundsException- ifindexis negative or greater than the length of this sequence, or ifcodePointOffsetis positive and fromindexthe substring startingindexhas fewer thancodePointOffsetcode point, or ifcodePointOffsetis negative andindexthe substring beforeindexless than the absolute valuecodePointOffsetcode point.
      • getChars

        public void getChars​(int srcBegin,
                             int srcEnd,
                             char[] dst,
                             int dstBegin)
        Copy characters from this sequence into the destination character arraydst 。 The first character to be copied is the indexsrcBegin ; The last character to be copied is located at indexsrcEnd-1 。 the total number of characters to be copied issrcEnd-srcBegin 。 characters are copied todstOf subdstfrom indexdstBeginStarts at and ends at the index:
           dstbegin + (srcEnd-srcBegin) - 1  
        Parameter
        srcBegin- Start copying at this offset.
        srcEnd- Stop copying at this offset.
        dst- The array into which the data is to be copied.
        dstBegin- offset todst 。
        Exception
        IndexOutOfBoundsException- if any of the following conditions is satisfied:
        • srcBeginis negative
        • dstBeginis negative
        • srcBeginthe argument is greater thansrcEndparameters.
        • srcEndgreater thanthis.length() 。
        • dstBegin+srcEnd-srcBegingreater thandst.length
      • setCharAt

        public void setCharAt​(int index,
                              char ch)
        The character at the specified index is set toch 。 The sequence is altered to represent a new character sequence identical to the old character sequence, except that it contains located atchcharacters ofindex 。

        The index parameter must be greater than or equal to0, and less than the length of this sequence.

        Parameter
        index- the index of the character to be modified.
        ch- The new character.
        Exception
        IndexOutOfBoundsException- ifindexis negative or greater than or equal to.length() 。
      • substring

        public String substring​(int start)
        returns a newString, which contains a subsequence of the characters currently contained in this character sequence. The substring starts at the specified index and extends to the end of this sequence.
        Parameter
        start- the starting index, inclusive.
        Result
        - The new string.
        Exception
        StringIndexOutOfBoundsException- ifstartless than zero, or greater than the length of this object.
      • subSequence

        public CharSequence subSequence​(int start,
                                        int end)
        Returns a new character sequence that is a subsequence of this sequence.

        The form of calling this method

           sb.subSequence(begin,&nbsp;end) 
        The behavior is exactly the same as calling
           sb.substring(begin,&nbsp;end) 
        This method is provided so that this class can implementCharSequenceInterface.
        Specified by:
        subSequenceIn the interfaceCharSequence
        Parameter
        start- the starting index, inclusive.
        end- the ending index, exclusive.
        Result
        the specified subsequence
        Exception
        IndexOutOfBoundsException- ifstartorendis negative, ifendgreater thanlength(), or ifstartgreater thanend
      • substring

        public String substring​(int start,
                                int end)
        returns a newString, containing a subsequence of the characters currently contained in this sequence. Substring from the specifiedstartstarts, and extends to the indexend - 1character at.
        Parameter
        start- the starting index, inclusive.
        end- the ending index, exclusive.
        Result
        - The new string.
        Exception
        StringIndexOutOfBoundsException- ifstartorendis negative or greater thanlength(), orstartgreater thanend 。
      • chars

        public IntStream chars()
        returninta stream of the elements in this sequencecharThe value is zero-extended. any mapping toa character that is a surrogate code pointare all uninterpreted.

        When a terminal stream operation begins, the stream is bound to the sequence (specifically, for mutable sequences, the stream'sspliteratorYeslate-binding )。 If the sequence is modified during the operation, the result is undefined.

        Specified by:
        charsIn the interfaceCharSequence
        Result
        An IntStream of char values from this sequence.
        Starting from the following version:
        9
      • codePoints

        public IntStream codePoints()
        Returns a stream of code point values from this sequence. Any surrogate pairs encountered in the sequence are combined, just likesame as Character.toCodePoint, the result is passed to the stream. Any other code unit (including ordinary BMP characters, unpaired surrogates, and undefined code units) will be zero-extended tointthe value, then passed to the stream.

        When a terminal stream operation begins, the stream is bound to the sequence (specifically, for mutable sequences, the stream'sspliteratorYeslate-binding )。 If the sequence is modified during the operation, the result is undefined.

        Specified by:
        codePointsIn the interfaceCharSequence
        Result
        The IntStream of Unicode code points points to this sequence.
        Starting from the following version:
        9