1.StringBuffer类
在以前我们学过String类,它是一个不可改变的类,一旦声明,这不可改变。即使改变也是改变的String的引用地址
如果想要经常改变字符串,那就要用StringBuffer类
我们知道String类是用"+"来连接字符串的,但是在StringBuffer确实用append方法来连接的字符串
下面就是一些StringBuffer常用的方法
1.字符串的连接
public
class
StringBufferDemo {
public
static
void
main(String[] args) {
StringBuffer stringBuffer=
new
StringBuffer();
stringBuffer.append(
"hello"
);
stringBuffer.append(
" world"
).append(
"!!!!"
);
System.
out
.println(stringBuffer);
}
}
结果:
hello world!!!!
2.在任意位置为StringBuffer添加内容
public
class
StringBufferDemo {
public
static
void
main(String[] args) {
StringBuffer stringBuffer=
new
StringBuffer();
stringBuffer.append(
"hello"
);
stringBuffer.append(
" world"
).append(
"!!!!"
);
System.
out
.println(stringBuffer);
stringBuffer.insert(0,
"like say"
);//在初始位置(第一个位置)插入
System.
out
.println(stringBuffer);
stringBuffer.insert(stringBuffer.length(),
"OK"
);//在最后插入
System.
out
.println(stringBuffer);
}
}
结果:
hello world!!!!
like sayhello world!!!!
like sayhello world!!!!OK
3.字符串的反转操作
public
class
StringBufferDemo {
public
static
void
main(String[] args) {
StringBuffer stringBuffer=
new
StringBuffer();
stringBuffer.append(
"hello"
);
stringBuffer.append(
" world"
);
System.
out
.println(stringBuffer);
String s= stringBuffer.reverse().toString();
//反转,在转为字符串
System.
out
.println(s);
}
}
结果:
hello world
dlrow olleh
4.替换指定范围的内容
public
class
StringBufferDemo {
public
static
void
main(String[] args) {
StringBuffer stringBuffer=
new
StringBuffer();
stringBuffer.append(
"hello"
);
stringBuffer.append(
" world"
);
System.
out
.println(stringBuffer);
stringBuffer.replace(6, 11,
"like"
);
//替换6到11的字符串
System.
out
.println(stringBuffer);
}
}
结果:
hello world
hello like
5.截取字符串
public
class
StringBufferDemo {
public
static
void
main(String [] args) {
StringBuffer stringBuffer=
new
StringBuffer();
stringBuffer.append(
"hello"
);
stringBuffer.append(
" world"
);
System.
out
.println(stringBuffer);
stringBuffer.replace(6, 11,
"like"
);
//替换6到11的字符串
System.
out
.println(stringBuffer);
String s= stringBuffer.substring(6,10);
//截取6到10的字符串
System.
out
.println(s);
}
}
结果:
hello world
hello like
like
6.删除指定位置的字符串
public
class
StringBufferDemo {
public
static
void
main(String[] args) {
StringBuffer stringBuffer=
new
StringBuffer();
stringBuffer.append(
"hello"
);
stringBuffer.append(
" world"
);
System.
out
.println(stringBuffer);
stringBuffer.replace(6, 11,
"like"
);
//替换6到11的字符串
System.
out
.println(stringBuffer);
String s= stringBuffer.substring(6,10);
//截取6到10的字符串
System.
out
.println(s);
System.
out
.println(stringBuffer);
stringBuffer.delete(6, 10);
//删除6到10的字符串
System.
out
.println(stringBuffer);
}
}
结果:
hello world
hello like
like
hello like
hello
7.查找指定的内容是否存在
public
static
void
main(String[] args) {
StringBuffer stringBuffer=
new
StringBuffer();
stringBuffer.append(
"hello"
);
stringBuffer.append(
" world"
);
System.
out
.println(stringBuffer);
stringBuffer.replace(6, 11,
"like"
);
//替换6到11的字符串
System.
out
.println(stringBuffer);
String s= stringBuffer.substring(6,10);
//截取6到10的字符串
System.
out
.println(s);
System.
out
.println(stringBuffer);
stringBuffer.delete(6, 10);
//删除6到10的字符串
System.
out
.println(stringBuffer);
System.
out
.println(stringBuffer.indexOf(
"hello"
));
//查找指定的字符串是否存在
System.
out
.println(stringBuffer.indexOf(
"like"
));
}
}
结果:
hello world
hello like
like
hello like
hello
0
-1
分析结果:0表示查找的字符串的起始位置是0开始的
-1表示没有找到