构造一个新的 StringBuffer 对象。
Optional
str: string初始字符串,可选。
Private
_strings_追加指定的字符串到当前 StringBuffer 实例中。
以下代码创建了一个 StringBuffer 实例,并连续调用了 append() 方法三次,向其添加了三个字符串。然后,我们调用 toString() 方法将这些字符串连接起来,并输出结果。可以看到,输出结果为 'Hello, world'。注意,每个字符串之间默认没有分隔符。
const sb = new StringBuffer()
sb.append('Hello')
.append(', ')
.append('world')
console.log(sb.toString()) // 'Hello, world'
要追加的字符串。
当前 StringBuffer 实例以支持链式调用。
清除当前 StringBuffer 实例中的所有字符串。
以下代码创建了一个 StringBuffer 实例,并连续调用了 append() 方法三次,向其添加了三个字符串。然后,我们调用 clear() 方法将实例中的所有字符串清除,再次输出结果。可以看到,输出结果为空字符串。注意,在调用 clear() 方法后,实例中的所有字符串都被清除,且不能恢复。
const sb = new StringBuffer()
sb.append('Hello')
.append(', ')
.append('world')
console.log(sb.toString()) // 'Hello, world'
sb.clear()
console.log(sb.toString()) // ''
当前 StringBuffer 实例以支持链式调用。
返回由当前 StringBuffer 实例中的所有字符串连接而成的一个字符串。
以下代码创建了一个 StringBuffer 实例,并连续调用了 append() 方法三次,向其添加了三个字符串。然后,我们先调用 toString() 方法将这些字符串连接起来,输出结果为 'Hello, world'。接着,我们再次调用 toString() 方法,并传入分隔符 ' | ',输出结果为 'Hello | world'。注意,在默认情况下,每个字符串之间没有分隔符。
const sb = new StringBuffer()
sb.append('Hello')
.append(', ')
.append('world')
console.log(sb.toString()) // 'Hello, world'
console.log(sb.toString(' | ')) // 'Hello | world'
可选参数,用来分隔每个字符串,默认为 ""。
由当前 StringBuffer 实例中的所有字符串连接而成的一个字符串。
Generated using TypeDoc
StringBuffer 类是一个用于构建字符串的辅助类。它允许你在不创建许多中间字符串的情况下,依次追加字符串并最终将它们连接成一个字符串。
Example