JAVA范例 六)字符串---StringBuffer

字符串缓存类StringBuffer 

实例106 创建字符串缓存类

package Chapter06.stringBuffer;
public class StringBufferDemo_01 {
	public static void main(String[] args) {
		String str = "StringBuffer";
		StringBuffer sb, sb1, sb2, sb3;
		sb = new StringBuffer();			// 创建一个空的字符串缓存区
		sb1 = new StringBuffer(50);		// 创建一个指定字符长度的字符串缓存区
		// 创建一个具有指定字符串内容的字符串缓存区
		sb2 = new StringBuffer("大家好");
		sb3 = new StringBuffer(str);
		// capacity()方法的主要作用是获取当前字符串的容量
		// length()方法的主要作有是获取当前字符串的长度
		System.out.println("创建StringBuffer类的方式一:");
		System.out.println("字符串sb的容量为:" + sb.capacity());
		System.out.println("字符串sb的长度为:" + sb.length());
		System.out.println("创建StringBuffer类的方式二:");
		System.out.println("字符串sb1的容量为:" + sb1.capacity());
		System.out.println("字符串sb1的长度为:" + sb1.length());
		System.out.println("创建StringBuffer类的方式三:");
		System.out.println("字符串sb2的容量为:" + sb2.capacity());
		System.out.println("字符串sb2的长度为:" + sb2.length());
		System.out.println("字符串sb3的容量为:" + sb3.capacity());
		System.out.println("字符串sb3的长度为:" + sb3.length());
	}
}

 

实例107 提取单个字符

package Chapter06.stringBuffer;
public class StringBufferDemo_02 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("1234567891011121314");
		String sum = "";
		for (int i = 0; i < sb.length(); i++) {
			if (i % 2 == 0) {
				sum = sum + sb.charAt(i);		// 获取指定位置的字符
			}
		}
		System.out.println("在StringBuffer中下标为偶数的字符串为:" + sum);
	}
}

 

实例108 给指定字符赋值

package Chapter06.stringBuffer;
public class StringBufferDemo_03 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("田田是一个女生");
		System.out.println("原字符串缓存区中的内容如下:\n   " + sb);
		sb.setCharAt(5, '男');		// 给指定下标位置上的字符付新值
		System.out.println("新字符串缓存区中的内容如下:\n   " + sb);
	}
}

 

实例109 插入新的字符

package Chapter06.stringBuffer;
public class StringBufferDemo_04 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("HELLOW");
		System.out.println("原字符串缓存区sb的信息如下:");
		System.out.println("其长度为:" + sb.length());
		System.out.println("其容量为:" + sb.capacity());
		System.out.println("其内容为:" + sb);
		for (int i = 0; i <= sb.length(); i += 2) {// 在原串中的每个字符前加上一个*
			sb.insert(i, '*');// 在指定的位置前插入字符*
		}
		System.out.println("\n新字符串缓存区sb的信息如下:");
		System.out.println("其长度为:" + sb.length());
		System.out.println("其容量为:" + sb.capacity());
		System.out.println("其内容为:" + sb);
	}
}

 

实例110 插入新的字符串 

package Chapter06.stringBuffer;

import java.util.Random;

public class StringBufferDemo_05 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("半江瑟瑟半江红");
		System.out.println("原字符串缓存区sb的信息如下:");
		System.out.println("其长度为:" + sb.length());
		System.out.println("其容量为:" + sb.capacity());
		System.out.println("其内容为:" + sb);
		Random rd = new Random();
		int n = rd.nextInt(sb.length());
		sb.insert(n, " ");			// 在指定的位置上插入一个空格
		sb.insert(n, 12.5);		// 在指定的位置上插入一个double型数据
		sb.insert(n, 10.2f);		// 在指定的位置上插入一个float型数据
		sb.insert(n, 88);			// 在指定的位置上插入一个int型数据
		System.out.println("\n新字符串缓存区sb的信息如下:");
		System.out.println("其长度为:" + sb.length());
		System.out.println("其容量为:" + sb.capacity());
		System.out.println("其内容为:" + sb);
	}
}

 

实例111 获取字符串的子串

package Chapter06.stringBuffer;
public class StringBufferDemo_06 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("This is a dog");
		String str = sb.substring(5);// 返回从指定的下标位置开始截取的子字符串
		String str1 = sb.substring(2, 4);// 返回从指定的两个下标位置之间截取的子字符串
		System.out.println("原字符串缓存区sb的内容为:" + sb);
		System.out.println("从下标为5的位置获取的子串为:" + str);
		System.out.println("从下标为2开始到下标为4结束,获取的子串为:" + str1);
	}
}

 

实例112 删除指定的字符 

package Chapter06.stringBuffer;
public class StringBufferDemo_07 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer(
				"Are you come from Japan? No, I come from China");
		sb.deleteCharAt(27);			// 将下标是27位置上的字符也就是逗号删除掉
		System.out.println("删除指定的单个字符后的内容为:\n    " + sb);
		sb.delete(0, 28);			// 将下标为0到28之间的子串全部删除。
		System.out.println("删除指定的子字符串后的内容为:\n    " + sb);
	}
}

 

实例113 倒置字符串 

package Chapter06.stringBuffer;
public class StringBufferDemo_08 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("他谢谢你");
		System.out.println("原字符串缓存区中的内容如下:\n  " + sb);
		sb.reverse();
		System.out.println("调用reverse方法后的内容如下:\n  " + sb);
	}
}

 

实例114 去除重复字符 

package Chapter06.stringBuffer;

import java.util.Random;

public class StringBufferDemo_09 {
	public static void main(String[] args) {
		String str = "";
		Random rd = new Random();
		for (int i = 0; i < 20; i++) {
			str = str + rd.nextInt(10);			// 生成一个由0-9之间的字符组成的字符串,其长度为20
		}
		StringBuffer sb = new StringBuffer(str);	// 根据指定的String对象创建StringBuffer对象
		System.out.println("根据随机生成的字符串创建的字符串缓存区sb的信息如下:");
		System.out.println("其长度为:" + sb.length());
		System.out.println("其容量为:" + sb.capacity());
		System.out.println("其内容为:" + sb);
		for (int i = 0; i < sb.length(); i++) {			// 判断当前字符是否与其他的字符相等
			for (int j = 0; j < sb.length(); j++) {
				if (sb.charAt(i) == sb.charAt(j)) {	// 如果相等,就删除与其相等的字符
					sb.deleteCharAt(j);
				}
			}
		}
		System.out.println("\n将重复的字符去掉后的新字符串缓存区sb的信息如下");
		System.out.println("其长度为:" + sb.length());
		System.out.println("其容量为:" + sb.capacity());
		System.out.println("其内容为:" + sb);
	}
}

 

实例115 检查是否是回文 

package Chapter06.stringBuffer;

import java.util.Scanner;

public class StringBufferDemo_10 {
	public static void main(String[] args) {
		System.out.println("请输入一个字符串:");
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();				// 获取从键盘上录入的字符串信息
		StringBuffer sb = new StringBuffer(str);	// 根据该字符串创建一个StringBuffer对象
		sb.reverse();						// 调用reverse方法,将字符串缓存区的内容倒置
		int n = 0;							// 计算相等字符的总数
		for (int i = 0; i < str.length(); i++) {
			// 如果字符串缓存区中每个字符与字符串str中的每个字符相等
			if (str.charAt(i) == sb.charAt(i)) { 
				n++;// n加1
			}
		}
		// 如果所有字符都相等的总数等于字符串str的长度,则str是回文否则不是回文
		if (n == str.length()) { 
			System.out.println(str + "是回文");
		} else {
			System.out.println(str + "不是回文");
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值