黑马程序员----------String,StringBuffer,StringBuilder用法

本文深入探讨Java中的字符串概念,包括字符串对象的特点、字符串常量池、String类的特性及使用方法,如字符串比较、操作和转换等,并介绍了StringBuffer与StringBuilder的区别及应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * Java 语言中最常见描述字符串的对象
 * 字符串数据都是一个对象
 * 字符串数据一旦初始化就不可以被改变了,双引号表示的都是字符串常量
 * 字符串对象都存储常量池中 字符串常量池
 * String 类不能有子类 不能被继承
 */
public class StringDemo {
	public static void main(String args[])
	{
		String s = "abcd"; //常量池
		//s ="haha"; //指向变了 而不是字符串改变
		String s1 ="abcd";
		String s3 = "ab"+"cd";
		String s2 = new String("abcd");//先堆内存创建对象 然后再常量池中创建对象"abcd"
		System.out.println(s==s1); //true
		System.out.println(s==s2); //false
		System.out.println(s==s3); //true
		System.out.println(s.equals(s1)); //true
		System.out.println(s.equals(s2));//true
		System.out.println(s3.equals(s2));//true
		//对于字符串的对象比较 应该用equals方法比较
		//String类覆盖了Object类中equals方法,用来比较字符串是否相等
		System.out.println("s: "+s+"s1: "+s1+"s2: "+s2);
		System.out.println("s: "+s.toString()+"s1: "+s1.toString()+"s2: "+s2.toString());
 	}


 
	/*
	 * 字符串对象应具备什么功能的操作
	 */
	public static void main(String args[]) {
		String str = "abcdefgh";
		System.out.println("字符串长度:" + str.length());
		System.out.println("字符串哈希码:" + str.hashCode());
		for (int i = 0; i < str.length(); i++) {
			System.out.print(str.charAt(str.length() - 1 - i));
		}
		System.out.println("字符串是否为empty:" + str.isEmpty());
		System.out.println(str.indexOf("b", 0)); // 没有为-1
		System.out.println(str.indexOf("bcde"));
		// System.out.println(str.charAt(8)); //字符串脚标越界异常
		// StringIndexOutOfBoundsException
		String s1 = str.substring(3);
		System.out.println(s1);
		String s2 = str.substring(3, 5); // 重载
		System.out.println(s2);
		String s3 = str.concat("abc"); // 将指定字符串(abc)连接到此字符串的结尾。
		System.out.println(s3);
		String s4 = str.substring(0, str.length());
		System.out.println(s4);
		String s5 = str.toUpperCase();
		System.out.println("转化成大写:" + s5);
 
public static void main(String args[])
	{
		String str ="abcdefg";
		String s =str.replace("abc","nba"); //替换字符串
		System.out.println(s);
		System.out.println(str);
		char arr[] =str.toCharArray();//将字符串转化为字符数组
		
		System.out.println(arr);
		boolean b =str.contains("abc");
		System.out.println(b);
		String name ="tang,yi,chao";
		String names[]=name.split(",");
		for (int i = 0; i < names.length; i++) {
			System.out.println(names[i]);
			
		}
		String hh ="  bbb  ccc";
		System.out.println(hh);
		String h=hh.trim();//  返回字符串的副本,忽略前导空白和尾部空白。
		System.out.println(h);
	}


//提取字符串中 小片段字符串“nba”的个数
public static void main(String args[]) {
		String str = "nbacccnbanbahhh,\\\nbadddnba";
		String key = "nba";
		
		int count = getSubCount(str, key);
		System.out.println("count:" + count);

	}

	public static int getSubCount(String str, String key) {
		int count = 0;
		int index = 0;
		// 调用indexOf方法获取key出现的位置
		while ((index = str.indexOf(key,index)) != -1) {
			index = index + key.length();
			count++;
		}

		return count;
	}

 

//反转字符串

//思路:字符串转化为数组 数组进行反转 将数组变成字符串
public static void main(String[] args) {

		/**
		 * 字符串转化为数组 数组进行反转 将数组变成字符串
		 */
		String s2 = "abcdeghljklmogjjkk";
		System.out.println("原来字符串:"+s2);
		s2 = reverseString(s2);
		System.out.println("反转字符串:"+s2);
	}

	private static String reverseString(String str) {
		// 将字符串转化为字符数组
		char[] chs = str.toCharArray();
		reverseArray(chs);
		return new String(chs);

	}

	/*
	 * 将一个字符数组进行反转
	 */

	private static void reverseArray(char[] chs) {
		// TODO Auto-generated method stub
		for (int start = 0, end = chs.length - 1; start < end; start++, end--) {
			swap(chs, start, end);
		}

	}

	private static void swap(char[] chs, int start, int end) {
		// TODO Auto-generated method stub
		char temp = chs[start];
		chs[start] = chs[end];
		chs[end] = temp;

	}


//对一个字符串进行字典顺序生成一个有序的字符串
public static void main(String args[]) {
		String s1 = "aAd/g=dD+-1";
		s1 = sortString(s1);
		System.out.println("字典排序后的:"+s1);
	}

	private static String sortString(String str) {
		// TODO Auto-generated method stub
		char[] chs = str.toCharArray();
		mySort(chs);
		return new String(chs);
	}

	private static void mySort(char[] chs) {
		// TODO Auto-generated method stub
		for (int x = 0; x < chs.length - 1; x++) {
			for (int y = x + 1; y < chs.length; y++)
				if (chs[x] > chs[y])
					swap(chs, x, y);
		}

	}

	private static void swap(char[] chs, int x, int y) {
		// TODO Auto-generated method stub
		char tmp = chs[x];
		chs[x] = chs[y];
		chs[y] = tmp;
	}


StringBuffer讲解:
* 字符串缓冲流
  * 线程安全的可变字符序列
  * 
  * 长度不够 再创建字符串一半或者一倍。 
  * 特点:
  * 缓冲区就是用来缓冲数据的 是一个容器
  * 1,长度可变
  * 2,线程安全
  * 3,提供了对容器中内容的操作的方法(增删改查)
  * StringBuffer()  构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。
 
 
 
public static void main(String args[])
	{
		StringBuffer sb =new StringBuffer();
		
		//使用其添加方法,往缓冲区添加添加元素 byte short 不能
		sb.append(1234);
		sb.append("tangyichao");
		sb.append(true);
		System.out.println(sb);
		sb.append(sb);
		//sb.append(sb);
		//sb.append(sb);
		//sb.append(sb);
		System.out.println(sb); //append 返回的还是原来的缓冲区
		//String str ="abc"+6+"c";
//			str =new StringBuffer().append("abc").append(6).append("c").toString();
		StringBuffer sb1 =new StringBuffer("abcdfeg");
//		sb1.insert(1, 123);
//		System.out.println(sb1);
//		sb1.delete(0, 1); // 包含头 不包含尾巴
//		System.out.println(sb1);
//		sb1.delete(0,sb1.length()); //清空缓冲区
//		System.out.println(sb1);
		sb1.replace(1, 3, "AK47");
		System.out.println(sb1);
		System.out.println(sb1.reverse()); //反转字符串

	}
用两种方法进行对数组转化为字符串
public static void main(String args[])
	{
		int [] arr={1,2,3,4,5};
		String s1=toString_1(arr);
		System.out.println(s1);
		String s2=toString_2(arr);
		System.out.println(s2);
	}
	private static String toString_1(int arr[])
	{
		String str ="[";
		for(int i=0;i<arr.length;i++)
		{
			if(i!=(arr.length-1))
			{
				str+=arr[i]+",";
			}else{
				str+=arr[i]+"]";
			}
		}
		return str;
	}
	private static String toString_2(int arr[])
	{
		StringBuffer str =new StringBuffer();
		str.append("[");
		for(int i=0;i<arr.length;i++)
		{
			if(i!=(arr.length-1))
			{
				str.append(arr[i]+",");
			}else{
				str.append(arr[i]+"]");
			}
		}
		return str.toString();
	}
/**
  * StringBuilder
  * JDK 1.5版本出现的一个新的字符串缓冲区
  * 功能和StringBuffer一样的
  * 单个线程建议用StringBuilder
  * StringBuffer 线程安全的
  * StringBuilder 线程不安全的
  * 提高效率 不用使用同步锁
  * 加锁 一个线程在执行容器中德内容是,其他线程不可以执行
  * 实际开发是:
  * 建议使用StringBuilder
  * 如果用到多线程 则使用StringBuffer
  * @param args
  */

  用StringBuffer对数组转化为字符串
 
private static String toString_2(int arr[])
	{
		StringBuilder str =new StringBuilder();
		str.append("[");
		for(int i=0;i<arr.length;i++)
		{
			if(i!=(arr.length-1))
			{
				str.append(arr[i]+",");
			}else{
				str.append(arr[i]+"]");
			}
		}
		return str.toString();
	}







                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值