String,StringBuffer

String

class String{
	public static void main(String[] args) {
		// 静态创建String
		String name; // null
		// 第一次赋值
		name = "jackma"; // jackma
		// 第二次赋值
		name = "tomlei"; // tomlei
		// 第三次
		name = name + "ssss"; // tomlei+sssss tomleissss
		System.out.println(name);

		// 动态创建 String
		// 静态创建String 可以多个引用指向同一个字符串兑现
		// 但是 动态创建String 不会 多个引用指向同一个字符串对象 因为 每次多是new 新对象
		String s = new String("this is String");
		String s1 = new String("this is String");

		String n = "HelloWorld";
		// indexOf 实现检索 返回第一个出现的值下标
		int index = n.indexOf("H");
		System.out.println(index);
		
		//substring 用于截取   
		String sub1=n.substring(5)//sbstring(starindex) 从某位截取到最后一位
		System.out.println(sub1);
		String sub2 =n.substring(0, 5);;//substring(starindex,endindex)截取 某位到某位
		System.out.println(sub2);
		
		// 去空格  tirm 
		String  w1="  sss  ";
		w1=w1.trim();    //它是一个有返回值的方法
		System.out.println(w1);
	}
}

StringBuffer

public class StringBufferTest01 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer();
		//1、append 追加的意思 用以 StringBufer添加数据
		sb.append(true);
		sb.append("abc");
		System.out.println(sb);
		
		//2、deletCharAt 该方法的作用是删除指定位置的字符,然后将剩余的内容形成新的字符串。
		sb.deleteCharAt(5);
		System.out.println(sb);
		
		//3、delet(index,end) 从第几位删除到第几位
		sb.delete(4, 6);   //包含头 不包含尾
		System.out.println(sb);
		
		//4、insert(index ,数据)该方法的作用是在StringBuffer对象中插入内容,然后形成新的字符串。
		sb.insert(2, "abc");//从选取的位置之后进行插入
		System.out.println(sb);
		
		//5、reverse  进行内容反转  倒序
		sb.reverse();
		System.out.println(sb);
		
		//6、setCharAt 该方法的作用是修改对象中索引值为index位置的字符为新的字符
		sb.setCharAt(0,'E');
		System.out.println(sb);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值