码迷,mamicode.com
首页 > 编程语言 > 详细

Java中StringBuffer类

时间:2020-02-16 20:38:55      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:ace   创建   字符   let   stat   爱我   static   string   sub   

StringBuffer:
线程安全的可变字符串。

StringBuffer和String的区别?
前者长度和内容可变,后者不可变。
如果使用前者做字符串的拼接,不会浪费太多的资源。

StringBuffer的构造方法:
public StringBuffei():无参构造方法
public StringBuffer(int capacity):指定容量的字符串缓冲区对象
public StringBuffer(String str):指定字符串内容的字符串缓冲对象

StringBuffer的方法:
public int capacity():返回当前容量。 理论值
public int length():返回长度(字符数)。 实际值

public static void main(String[] args) {
//public StringBuffei():无参构造方法
StringBuffer sb = new StringBuffer();
System.out.println("sb:"+sb);//sb:
System.out.println("sb.capacity():"+sb.capacity());//16
System.out.println("sb.length():"+sb.length());//0

//public StringBuffer(int capacity):指定容量的字符串缓冲区对象
StringBuffer sb2 = new StringBuffer(50);
System.out.println("sb2:"+sb2);//sb2:
System.out.println("sb2.capacity():"+sb2.capacity());//50
System.out.println("sb2.length():"+sb2.length());//0

//public StringBuffer(String str):指定 字符串内容的字符串缓冲对象
StringBuffer sb3 = new StringBuffer("hello");
System.out.println("sb3:"+sb3);//sb3:hello
System.out.println("sb3.capacity():"+sb3.capacity());//21
System.out.println("sb3.length():"+sb3.length());//5
}

/**StringBuffer的添加功能:*/
//public StringBuffer append(String str):可以把任意类型添加到字符串缓冲区里面

public static void main(String[] args) {
//创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();

//public StringBuffer append(String str)
StringBuffer sb2 = sb.append("hello");
System.out.println("sb:"+sb);//hello
System.out.println("sb2:"+sb2);//hello
System.out.println(sb == sb2);//true

//使用append一步一步的添加数据
sb.append("hello");
sb.append("true");
sb.append("12");
sb.append("34.56");
System.out.println(sb);//hellohellotrue1234.56

//使用链式编程
sb.append("hello").append("true").append("12").append("34.56");
System.out.println(sb);//hellohellotrue1234.56

//public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
sb.insert(5,"world");
System.out.println(sb);//helloworldhellotrue1234.56
sb.insert(2,"world");
System.out.println(sb);//heworldllohellotrue1234.56
}

/**StringBuffer的删除功能*/
//public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
//public StringBuffer delete(int start,int end):删除从指定位置开始到指定位置结束的功能,并返回本身

public static void main(String[] args) {
//创建对象
StringBuffer sb = new StringBuffer();

//先对空的sb进行添加
sb.append("hello").append("world").append("java");
System.out.println("sb:"+sb);//helloworldjava

//public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
//需求:删除e字符
sb.deleteCharAt(1);//hlloworldjava
//如果这个时候我还要删除第一个l 因为值已经变成了hlloworldjava 所以 还是索引值还是1
sb.deleteCharAt(1);//hloworldjava

//使用public StringBuffer delete(int start,int end):删除从指定位置开始到指定位置结束的功能,并返回本身
StringBuffer sb = new StringBuffer("helloworldjava");
sb.delete(5,10);//hellojava //包左不包右
sb.delete(0,sb.length());//没有值 都删除了
}
/**StringBuffer的替换功能*/
//public StringBuffer replace(int start,int end,String str):从start开始到end用str替换

public static void main(String[] args) {
//创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();

//添加数据
sb.append("hello");
sb.append("world");
sb.append("java");

//public StringBuffer replace(int start,int end,String str):从start开始到end用str替换
//需求:我要将world替换为"节日快乐"
sb.replace(5,10,"节日快乐");//hello节日快乐java
}
/**StringBuffer的反转功能*/
//public StringBuffer reverse():反转字符串

public static void main(String[] args) {
//创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();

//添加数据
sb.append("我爱java,java不爱我"):
System.out.println(sb);//我爱java,java不爱我

//public StringBuffer reverse()
sb.reverse();
System.out.println(sb);//我爱不avaj,avaj爱我
}
/**StringBuffer的截取功能*/
//public String substring(int start):输入索引 索引之前的元素将会被截取调 索引本身不会
//public String substring(int start,int end):截取索引之间的数,包左不包右 这两个都不会改变原来的值
public static void main(String[] args) {
//创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();
System.out.println(sb);//helloworldjava

//添加数据
sb.append("hello").append("world").append("java");

//截取功能
//public String substring(int start)
String s = sb.substring(5);
System.out.println(s);//worldjava
System.out.println(sb);//helloworldjava
}
public static void main(String[] args) {
//创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();
System.out.println(sb);//helloworldjava

//添加数据
sb.append("hello").append("world").append("java");

//截取功能
//public String substring(int start,int end)
String s = sb.substring(5,10);
System.out.println(s);//world
System.out.println(sb);//helloworldjava
}

/**StringBuffer和String的相互转换*/
//那我们为什么要用类之间的转换呢?
A -- B的转换
我们把A转换为B,其实是为了使用B的功能。
B -- A的转换
我们可能要的结果是A类型,所以还得转回来。

public static void main(String[] args) {
//String -- StringBuffer
String s = "hello";

//注意:不能把字符串的值直接赋值给StringBuffer
//StringBuffer sb = "hello";
//StringBuffer sb = s;//应该用下面两种方式

//方式1:通过构造方法
StringBuffer sb = new StringBuffer(s);
//方式2:通过append()方法:
StringBuffer sb2 = new StringBuffer();
sb2.append(s);

System.out.println(sb);//hello
System.out.println(sb2);//hello
//StringBuffer -- String
StringBuffer buffer = new StringBuffer("java");

//将StringBuffer类型的buffer转换为String类型有下面两种方法
//String(StringBuffer buffer)
//方式1:通过构造方法
String str = new String(buffer);
//方式2:通过toString()方法
String str2 = buffer.toString();
System.out.println(str);//java
System.out.println(str2);//java
}


StringBuffer的面试题:

1.//String,StringBuffer,StringBuilder的区别?
答:A:String是内容是不可变的,而StringBuffer,StringBuilder都是内容可变的。
B:StringBuffer是同步的,数据安全,效率低,StringBuilder是不同步的,数据不安全,效率高。

2.//StringBuffer和数组的区别?
答:二者都可以看出是一个容器,装其他的数据。
但是,StringBuffer的数据最终是一个字符串数据。
而数组可以安置多种数据,但必须是同一种类型。

3.//形式参数问题 String作为参数传递 StringBuffer作为参数传递?
形式参数:
基本类型:形式参数的改变不影响实际参数
引用类型:形式参数的改变直接影响实际参数
注意:
String作为参数传递,效果和基本类型作为参数传递是一样的。

代码:
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
System.out.println(s1 + "---" + s2);// hello world
change(s1, s2);
System.out.println(s1 + "---" + s2);// hello world

StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("world");
System.out.println(sb1 + "---" + sb2);// hello world
change(sb1, sb2);
System.out.println(sb1 + "---" + sb2);// hello worldworld

}

private static void change(StringBuffer sb1, StringBuffer sb2) {
sb1 = sb2;
sb2.append(sb1);
}

private static void change(String s1, String s2) {
s1 = s2;
s2 = s1 + s2;
}

/**字符串判断对称功能的案例*/
public class Atest {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据");
String a = sc.nextLine();

boolean falg = isScme(a);
System.out.println(falg);
}

public static boolean isScme(String a) {
char[] arr = a.toCharArray();
for (int start = 0, end = arr.length - 1; start <= end; start++, end--) {
if (arr[start] != arr[end]) {
return false;
}
}
return true;
}
}
改进:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据");
String a = sc.nextLine();

boolean falg = isScme(a);
System.out.println(falg);
}

public static boolean isScme(String a) {

return new StringBuffer(a).reverse().toString().equals(a);

}

Java中StringBuffer类

标签:ace   创建   字符   let   stat   爱我   static   string   sub   

原文地址:https://www.cnblogs.com/lszbk/p/12318426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!