Java 怎么演示StringBuilder insert, delete 跟 deleteCharAt 方法用法

2019年3月29日18:54:13 评论 162

1.简介

StringBuilder insert、 delete 跟 deleteCharAt 方法,这三个方法怎么用?请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */
public class StringBuilderMethods2 {
	public static void main(String[] args) {
		Object objectRef = "hello";
		String string = "world";
		char[] charArray = {'a', 'b', 'c', 'd', 'e', 'f'};
		boolean booleanValue = true;
		char characterValue = 'K';
		int integerValue = 7;
		long longValue = 10000000;
		float floatValue = 2.5f;
		double doubleValue = 33.333;

		StringBuilder stringBuilder = new StringBuilder();

		stringBuilder.insert(0, objectRef).insert(0, "  ").insert(0, string).insert(0, "  ")
				.insert(0, charArray).insert(0, "  ").insert(0, charArray, 3, 3)
				.insert(0, "  ").insert(0, booleanValue).insert(0, "  ").insert(0, characterValue)
				.insert(0, "  ").insert(0, integerValue).insert(0, "  ").insert(0, longValue)
				.insert(0, "  ").insert(0, floatValue).insert(0, "  ").insert(0, doubleValue);

		System.out.printf("stringBuilder 插入后:\n%s\n\n", stringBuilder.toString());

		stringBuilder.deleteCharAt(10);
		stringBuilder.delete(2, 6);

		System.out.printf("stringBuilder 删除后:\n%s\n", stringBuilder.toString());
	}
}

3.结果

4.总结

StringBuilder insert 方法插入内容,是个重载函数,支持参数多样; delete 方法删除指定的起始索引以及终止索引范围内的内容; deleteCharAt 方法删除指定索引的内容。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: