首页编程javastringbuilder(java中的StringBuilder有什么用)

javastringbuilder(java中的StringBuilder有什么用)

编程之家2023-11-03135次浏览

大家好,今天来为大家分享javastringbuilder的一些知识点,和java中的StringBuilder有什么用的问题解析,大家要是都明白,那么可以忽略,如果不太清楚的话可以看看本篇文章,相信很大概率可以解决您的问题,接下来我们就一起来看看吧!

javastringbuilder(java中的StringBuilder有什么用)

java中StringBuilder的长度问题

StringBuffer是可变类,和线程安全的字符串操作类,任何对它指向的字符串的操作都不会产生新的对象。每个StringBuffer对象都有一定的缓冲区容量,当字符串大小没有超过容量时,不会分配新的容量,当字符串大小超过容量时,会自动增加容量。

StringBuffer buf=new StringBuffer();//分配长16字节的字符缓冲区

StringBuffer buf=new StringBuffer(512);//分配长512字节的字符缓冲区

StringBuffer buf=new StringBuffer("this is a test")//在缓冲区中存放了字符串,并在后面预留了16字节的空缓冲区。

注意,是16字节的字符缓冲区,不是长度,length是取出字符串的长度,创建该对象时,没有字符串,自然长度就是0了

java中的StringBuilder有什么用

StringBuilder叫字符串缓存区对象,使用时可以不断添加字符、字符串等已有内容。

javastringbuilder(java中的StringBuilder有什么用)

需要使用时调用toString()获得对象里的所有字符串。

作用:

简化字符串常量池的个数,节省内存。

StringBuilder相当于一个池子,可以存放很多已有的字符串,使用时可以以此获得很长的字符串,从而优化程序。

JDK底层使用StringBuilder实现字符串拼接。

JAVA 中Stringbuilder类的方法

StringBuilderpublic StringBuilder()构造一个其中不带字符的字符串生成器,初始容量为 16个字符。 StringBuilderpublic StringBuilder(int capacity)构造一个其中不带字符的字符串生成器,初始容量由 capacity参数指定。参数: capacity-初始容量。抛出: NegativeArraySizeException-如果 capacity参数小于 0。StringBuilderpublic StringBuilder(String str)构造一个字符串生成器,并初始化为指定的字符串内容。该字符串生成器的初始容量为 16加上字符串参数的长度。参数: str-缓冲区的初始内容。抛出: NullPointerException-如果 str为 nullStringBuilderpublic StringBuilder(CharSequence seq)构造一个字符串生成器,包含与指定的 CharSequence相同的字符。该字符串生成器的初始容量为 16加上 CharSequence参数的长度。参数: seq-要复制的序列。抛出: NullPointerException-如果 seq为 null方法详细信息appendpublic StringBuilder append(Object obj) Appends the string representation of the Object argument. The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this sequence.参数: obj- an Object.返回: a reference to this object.另请参见: String.valueOf(java.lang.Object), append(java.lang.String)</dd>appendpublic StringBuilder append(String str) Appends the specified string to this character sequence. The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument. If str is null, then the four characters"null" are appended. Let n be the length of this character sequence just prior to execution of the append method. Then the character at index k in the new character sequence is equal to the character at index k in the old character sequence, if k is less than n; otherwise, it is equal to the character at index k-n in the argument str.参数: str- a string.返回: a reference to this object.</dd>appendpublic StringBuilder append(StringBuffer sb)将指定的 StringBuffer添加到此序列。按顺序将 StringBuffer参数中的字符添加到该序列中,使该序列在长度上增加该参数的长度。如果 sb为 null,则向该序列中添加 4个"null"字符。在执行 append方法前,让此字符序列的长度为 n。如果 k小于 n,则新字符序列中索引 k处的字符等于原有序列中索引 k处的字符;否则它等于参数 sb中索引 k-n处的字符。参数: sb-要添加的 StringBuffer。返回:此对象的一个引用。</dd>appendpublic StringBuilder append(CharSequence s)从接口 Appendable复制的描述向此 Appendable添加指定的字符序列。有时可能没有添加整个序列,这取决于使用哪个类来实现字符序列 csq。例如,如果 csq是 CharBuffer的一个实例,则通过缓冲区的位置和限制来定义要添加的子序列。指定者:接口 Appendable中的 append参数: s-要添加的字符串序列。如果 csq为 null,则向该 Appendable添加四个字符"null"。返回:此 Appendable的引用抛出: IndexOutOfBoundsException</dd>appendpublic StringBuilder append(CharSequence s, int start, int end) Appends a subsequence of the specified CharSequence to this sequence. Characters of the argument s, starting at index start, are appended, in order, to the contents of this sequence up to the(exclusive) index end. The length of this sequence is increased by the value of end- start. Let n be the length of this character sequence just prior to execution of the append method. Then the character at index k in this character sequence becomes equal to the character at index k in this sequence, if k is less than n; otherwise, it is equal to the character at index k+start-n in the argument s. If s is null, then this method appends characters as if the s parameter was a sequence containing the four characters"null".指定者:接口 Appendable中的 append参数: s- the sequence to append. start- the starting index of the subsequence to be appended. end- the end index of the subsequence to be appended.返回: a reference to this object.抛出: IndexOutOfBoundsException- if start or end are negative, or start is greater than end or end is greater than s.length()</dd>appendpublic StringBuilder append(char[] str) Appends the string representation of the char array argument 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 exactly as if the argument were converted to a string by the method String.valueOf(char[]) and the characters of that string were then appended to this character sequence.参数: str- the characters to be appended.返回: a reference to this object.</dd>appendpublic StringBuilder append(char[] str, int offset, int len) Appends the string representation of a subarray of the char array argument to this sequence. Characters of the char array str, starting at index offset, are appended, in order, to the contents of this sequence. The length of this sequence increases by the value of len. The overall effect is exactly as if the arguments were converted to a string by the method String.valueOf(char[],int,int) and the characters of that string were then appended to this character sequence.参数: str- the characters to be appended. offset- the index of the first char to append. len- the number of chars to append.返回: a reference to this object.</dd>appendpublic StringBuilder append(boolean b) Appends the string representation of the boolean argument to the sequence. The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this sequence.参数: b- a boolean.返回: a reference to this object.另请参见: String.valueOf(boolean), append(java.lang.String)</dd>appendpublic StringBuilder append(char c) Appends the string representation of the char argument to this sequence. The argument is appended to the contents of this sequence. The length of this sequence increases by 1. The overall effect is exactly as if the argument were converted to a string by the method String.valueOf(char) and the character in that string were then appended to this character sequence.指定者:接口 Appendable中的 append参数: c- a char.返回: a reference to this object.</dd>appendpublic StringBuilder append(int i) Appends the string representation of the int argument to this sequence. The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this sequence.参数: i- an int.返回: a reference to this object.另请参见: String.valueOf(int), append(java.lang.String)</dd>appendpublic StringBuilder append(long lng) Appends the string representation of the long argument to this sequence. The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this sequence.参数: lng- a long.返回: a reference to this object.另请参见: String.valueOf(long), append(java.lang.String)</dd>appendpublic StringBuilder append(float f) Appends the string representation of the float argument to this sequence. The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string sequence.参数: f- a float.返回: a reference to this object.另请参见: String.valueOf(float), append(java.lang.String)</dd>appendpublic StringBuilder append(double d) Appends the string representation of the double argument to this sequence. The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this sequence.参数: d- a double.返回: a reference to this object.另请参见: String.valueOf(double), append(java.lang.String)</dd>appendCodePointpublic StringBuilder appendCodePoint(int codePoint) Appends the string representation of the codePoint argument to this sequence. The argument is appended to the contents of this sequence. The length of this sequence increases by Character.charCount(codePoint). The overall effect is exactly as if the argument were converted to a char array by the method Character.toChars(int) and the character in that array were then appended to this character sequence.参数: codePoint- a Unicode code point返回: a reference to this object.从以下版本开始: 1.5</dd>deletepublic StringBuilder delete(int start, int end) Removes the characters in a substring of this sequence. The substring begins at the specified start and extends to the character at index end- 1 or to the end of the sequence if no such character exists. If start is equal to end, no changes are made.参数: start- The beginning index, inclusive. end- The ending index, exclusive.返回: This object.抛出: StringIndexOutOfBoundsException- if start is negative, greater than length(), or greater than end.</dd></dd></dl></dl>

javastringbuilder(java中的StringBuilder有什么用)

非常感谢您的阅读!我们希望本文对于解决您关于javastringbuilder的问题提供了一些有价值的信息。如果您还有其他疑问,我们将很乐意为您提供进一步的帮助。

控制台应用程序?什么是应用程序和控制台程序两者有什么区别和联系网站推广专家,自己怎么做网站推广