StringBuffer class in JAVA common method Explanation

String class is unchanged, modified with String string creates a new String object, if frequent changes, will generate a lot of String objects, much overhead, so java provides a StringBuffer class, in terms of modifying the string String efficient than a lot higher.

There are three classes in java to be responsible for the operation of the character.

1.Character operation is a single character,

2.String to operate a string of characters. Immutable class.

3.StringBuffer also operates a string of characters, but the variable type.

{class UsingStringBuffer public
/ **
* Find matching string
* /
public static void testFindStr () {
the StringBuffer the StringBuffer new new SB = ();
sb.append ( "the StringBuffer This IS A");
// returns the substrings in the string position first appears, and if not, returns negative
System.out.println ( "sb.indexOf (" iS ") =" + sb.indexOf ( "iS"));
// set parameters to indexOf method, designated matching the start position
System.out.println ( "sb.indexOf (" iS ") =" + sb.indexOf ( "iS",. 3));
// returns the substring positions in the last occurrence of the string, If not, a negative return
System.out.println ( "sb.lastIndexOf (" iS ") =" + sb.lastIndexOf ( "iS"));
// set parameters to lastIndexOf method, matching the specified end position
System.out .println ( "sb.lastIndexOf (" IS ",. 1) ="
+ sb.lastIndexOf ( "IS",. 1));
}

/**
 * 截取字符串
 */
public static void testSubStr() {
	StringBuffer sb = new StringBuffer();
	sb.append("This is a StringBuffer");
	// 默认的终止位置为字符串的末尾
	System.out.print("sb.substring(4)=" + sb.substring(4));
	// substring方法截取字符串,可以指定截取的起始位置和终止位置
	System.out.print("sb.substring(4,9)=" + sb.substring(4, 9));
}

/**
 * 获取字符串中某个位置的字符
 */
public static void testCharAtStr() {
	StringBuffer sb = new StringBuffer("This is a StringBuffer");
	System.out.println(sb.charAt(sb.length() - 1));
}

/**
 * 添加各种类型的数据到字符串的尾部
 */
public static void testAppend() {
	StringBuffer sb = new StringBuffer("This is a StringBuffer!");
	sb.append(1.23f);
	System.out.println(sb.toString());
}

/**
 * 删除字符串中的数据
 */
public static void testDelete() {
	StringBuffer sb = new StringBuffer("This is a StringBuffer!");
	sb.delete(0, 5);
	sb.deleteCharAt(sb.length() - 1);
	System.out.println(sb.toString());
}

/**
 * 向字符串中插入各种类型的数据
 */
public static void testInsert() {
	StringBuffer sb = new StringBuffer("This is a StringBuffer!");
	// 能够在指定位置插入字符、字符数组、字符串以及各种数字和布尔值
	sb.insert(2, 'W');
	sb.insert(3, new char[] { 'A', 'B', 'C' });
	sb.insert(8, "abc");
	sb.insert(2, 3);
	sb.insert(3, 2.3f);
	sb.insert(6, 3.75d);
	sb.insert(5, 9843L);
	sb.insert(2, true);
	System.out.println("testInsert: " + sb.toString());
}

/**
 * 替换字符串中的某些字符
 */
public static void testReplace() {
	StringBuffer sb = new StringBuffer("This is a StringBuffer!");
	// 将字符串中某段字符替换成另一个字符串
	sb.replace(10, sb.length(), "Integer");
	System.out.println("testReplace: " + sb.toString());
}

/**
 * 将字符串倒序
 */
public static void reverseStr() {
	StringBuffer sb = new StringBuffer("This is a StringBuffer!");
	System.out.println(sb.reverse()); // reverse方法将字符串倒序
}

}
Summary:
StringBuffer class is not the same, when you modify the string contents, without creating a new object, so it is more suitable than the modified string String class.
StringBuffer class does not provide the same with the method toCharArray String
replace method StringBuffer class with different class String replace method, it has three parameters replace method, a first argument specifies a starting position of the substring is replaced, the second argument specifies substring is replaced with an end position, the third argument specifies the new substring

Published 254 original articles · won praise 23 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_30242987/article/details/104697652