public class StringBufferTest {
public static void main(String[] args) {
//以下为StringBuffer字符串缓冲池对象的常用方法。
//1.append()方法 追加方法
//StringBuffer可以将字符串变量进行多次变化,并且不用在方法去内存中占用空间。
StringBuffer buffer = new StringBuffer(16);
buffer.append("我是你的爱人").append("&").append(13).append(14.0D).append(true);
//2.toString()方法
// StringBuffer要是想输出必须通过toString方法转换成字符串,然乎才能输出
String str1=buffer.toString();
System.out.println(str1);
//3.insert(插入位置,插入元素) 插入方法 与append类似 不过需要加一个位置参数
StringBuffer buffer2=new StringBuffer("我是中国人");
buffer2.insert(2,"一个");
System.out.println(buffer2.toString());
//4.delete删除某个子串 deleteCharAt删除某个字符 删除方法
buffer2.delete(0,1);
System.out.println(buffer2.toString());
buffer.deleteCharAt(2);
System.out.println(buffer.toString());
//5.reverse() 翻转字符串 顾名思义就是把一个字符串翻转过来
buffer2.reverse();
System.out.println(buffer2.toString());
//6.replace(int start,int end,String str) 字符串的替换
buffer.replace(0,1,"我刘博展");
System.out.println(buffer.toString());
}
}
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
public class StringTest {
public static void main(String[] args) {
//以下是对String常用方法的练习,共计21个常用方法
//一、字符串的截取方法
//1.CharAt 方法:用来截取字符串单个字符
System.out.println("我是中国人".charAt(2));
//2.substring方法用来截取整个子串
System.out.println("我爱吃肉肉,你爱吃臭臭".substring(3));
System.out.println("我爱吃肉肉,你爱吃臭臭".substring(3,7));
//二、字符串的比较方法
//3.equals方法
String s1="我喜欢你";String s2="我喜欢你";
System.out.println(s1.equals(s2));
//4.equalsIgnoreCase()方法
System.out.println("abCd".equalsIgnoreCase("abcd"));
//5.compareTo()方法
//按胜负比,一匹配成功就不比了,-1就是不匹配,0就是匹配成功,正数就是相差多少
int temp="acd".compareTo("acdq");
System.out.println(temp);
int temp1="acd".compareTo("ac");
System.out.println(temp1);
//6.compareToIgnoreCase() 方法
System.out.println("SAve".compareToIgnoreCase("save"));
//三、判断是否以某个子串开头或结尾的方法
//7.endsWith()方法 用来判断字符串是否以某个子串结尾
System.out.println("你爱我啊我爱你".endsWith("我爱你"));
//8.startsWith()方法 用来判断字符串是否以某个子串开头
System.out.println("我是刘博展".startsWith("我是"));
//四、求字符串长度的方法
//9.length()方法 用来判断字符串的长度
System.out.println("我是刘博展".length());
//10.isEmpty()方法 用来判断字符串是否为空
System.out.println("www".isEmpty());
//五、字符串转换大小写的方法
//11.toLowwerCase()转换为小写
System.out.println("WWW.".toLowerCase());
//12.toUpperCase()转换为大写
System.out.println("qaq".toUpperCase());
//六.清除字符串前面和后面空格的方法
//13.trim().
System.out.println(" 我是你大哥 ".trim());
//七.将字符串变为数组的方法
//14.toCharArray()
char []ch="我是java工程师".toCharArray();
for(int i=0;i