JAVA 中Stringbuilder类的方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA 中Stringbuilder类的方法相关的知识,希望对你有一定的参考价值。

类似 append ,insert,delete等都是什么意思~简单举例~还有最后输出时候用的.toString是什么意思~需要详细的资料谢谢

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> 参考技术A append 是追加的意思 insert是替换的意思 delete是删除 toString是 将Stringbuilder转换成String 参考技术B Stringbuilder是一个字符缓冲的类.和StringBuffer是一样的.但是Stringbuilder是线程安全的 参考技术C 如果程序对附加字符串的需求很频繁,不建议使用+来进行字符串的串联,而应该使用java.lang.StringBuilder类,从而使效率大大提高。1、append 就是将信息追加到当前 StringBuilder 的结尾,例如:StringBuilder sb=new StringBuilder("abc");sb.append("123");System.out.println(sb);//输出abc1232、Insert 就是将字符串或对象插入到当前 StringBuilder 对象的指定索引处,例如:StringBuilder sb=new StringBuilder("abc");sb.insert(2, "123");
System.out.println(sb);//输出ab123c3、delete 就是删除指定开始索引到结束索引处的字符,作用等同于remove方法,例如:StringBuilder sb=new StringBuilder("abc");sb.delete(0, 1);//删除从索引0到1的字符
System.out.println(sb);//输出bc4、replace 就是替换指定索引处的指定字符,例如:StringBuilder sb=new StringBuilder("abc");sb.replace('b', 'B');
System.out.println(sb);//输出aBc另外,你说的toString是每个继承于Object对象的类都有的方法,作用是将对象转换为显示的字符串,SringBuilder重写了这个方法,返回StringBuilder对象的字符串内容。在使用System.out.println()输出对象时,如果不显示调用toString方法,该方法会被自动调用。
参考技术D

例如:在下面的示例代码中,创建了 StringBuilder 对象,用来存储字符串,并对其做了追加和插入操作。这些操作修改了 str 对象的值,而没有创建新的对象,这就是 StringBuilder 和 String 最大的区别。

运行结果:

来源:慕课网

以上是关于JAVA 中Stringbuilder类的方法的主要内容,如果未能解决你的问题,请参考以下文章

JAVA 中Stringbuilder类的方法

大数据必学Java基础(五十):String类和StringBuilder类的讲解

JAVA中String和StringBuilder类的特点及使用

Java全栈JavaSE:17.常用类之Object类String类StringBuilder类

Java 常用API

String类的常用方法+StringBuilder