String、StringBuffer、StringBuffer的一些常用方法和应用

1. String类

public class practice3 {
	public static void main(String[] args) {
		String s1="HelloWorld";
//char charAt(int index) 输入字符串对应下标获取该下标对应的字符
		char ch = s1.charAt(5);
		System.out.println(ch);   /* w */
//int indexOf(int ch)	输入字符获取对应的下标
		int a = s1.indexOf('o');
		System.out.println(a); /* 4 */
//int indexOf(String str) 输入的字符串位于哪个位置
		int c =s1.indexOf("llo");
		System.out.println(c);/* 2 */
//int indexOf(int ch,int fromIndex)	
//从fromIndex索引开始,查找第一次出现ch字符的索引
		int b = s1.indexOf('r', 1);
		System.out.println(b);/* 7 */
// int indexOf(String str,int fromIndex)	
//	从fromIndex索引开始,查找第一次出现str字符串的索引	
		int d = s1.indexOf("lloW", 1);	
		System.out.println(d);/* 2 */
//int lastIndexOf(int ch)  从后面数字符ch的索引
		int lastIndexOf = s1.lastIndexOf('W');
		System.out.println(lastIndexOf);/* 5 */
//String substring(int start)  输出从start到end内的字符串;左闭右开;
		System.out.println(s1.substring(5, 7));
		System.out.println(s1.substring(2));		
	}
}
public class practice {
	public static void main(String[] args) {
		String s1= "HelloWorld123";
//	boolean isEmpty():判断字符串是否为空
		System.out.println( s1.isEmpty());/*false*/
//	boolean equals(Object obj):将此字符串的内容与指定的对象比较,区分大小写
		String s2 =  "abc";
		System.out.println(s1.equals(s2));/*false*/
//boolean equalsIgnoreCase(String str):
//将此 String 与另一个 String 比较,忽略大小写。
		String s3 = new String(new char[] {'1','2','3'});
		System.out.println(s1.equalsIgnoreCase(s3));/* false */
//		boolean contains(String str):判断字符串中是否包含方法传入的字符串。
		System.out.println(s1.contains("Hello"));/* true */
//boolean startsWith(String str):判断字符串是否以某个指定的字符串开头。		
//boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
		System.out.println(s1.startsWith("h"));/* false */
		System.out.println(s1.endsWith("3"));/*true*/		
	}	
}
import java.util.Arrays;
public class practice7 {
	public static void main(String[] args) {
		String s1= "Aabc123456";
//		 byte[] getBytes() :将字符串转化为字节数组。
		System.out.println(Arrays.toString(s1.getBytes()));
//       char[] toCharArray(): 将字符串转化为字符数组。
		char[] ch = s1.toCharArray();
		for (char c : ch) {
			System.out.print("  "+c);
		}
		System.out.println("");
//static String valueOf(char[] chs): 返回 char 数组参数的字符串表示形式。
		System.out.println(s1.valueOf(false));
		System.out.println(s1.valueOf(5));
//String toLowerCase() :将此 String 中的所有字符都转换为小写。
//String toUpperCase() :将此 String 中的所有字符都转换为大写
		System.out.println(s1.toLowerCase());
		System.out.println(s1.toUpperCase());
//	String concat(String str): 将指定字符串连接到此字符串的结尾。
		System.out.println(s1.concat("我爱你"));
		System.out.println();
	}
}
public class practice{
	public static void main(String[] args) {
//		 String replace(char old,char new) :替换功能。
//		String replace(String old,String new) :替换功能。
		String s1= "HelloWorld";
		System.out.println(s1.replace('e', 'P'));
		System.out.println(s1.replace('o', 's'));
		System.out.println(s1.replaceAll("Hello", "replacement"));
//	String trim():去除字符串两空格。	
		String s2=  " "+"我爱中国"+"   ";
		System.out.println(s2);
		System.out.println(s2.trim());
	}
}
总结:利用String可进行字符串的修改、分割、获取、替换等功能;

2.StringBuffer:

  • StringBuffer类概述
    线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。虽然在任意 时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。

  • 构造方法
    (1) StringBuffer() :构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。
    (2)StringBuffer(CharSequence seq):构造一个字符串缓冲区,它包含与指定CharSequence 相同的字符。
    (3)StringBuffer(int capacity) :构造一个不带字符,但具有指定初始容量的字符串缓冲区。
    (4)StringBuffer(String str):构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。

/*
 *  增 :
	StringBuffer append(String str) 
	StringBuffer insert(int offset, String str)
	删 :
	StringBuffer deleteCharAt(int index) 
	StringBuffer delete(int start, int end) 
	改:
	public StringBuffer replace(int start,int end,String str)
	其他:
	public StringBuffer reverse() 
	public String substring(int start) 注意:截取操作不会影响到StringBuffer本身
	public String substring(int start,int end)
 */
public class StringBufferDemo02 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer();
//		StringBuffer sb2 = sb.append("Hello");
//		System.out.println(sb); // 
//		System.out.println(sb2); // hello
		sb.append("Hello");
		System.out.println(sb);
		
		// 链式调用
		sb.append('c').append("world").append(new Object());
		System.out.println(sb);
		
//		StringBuffer insert(int index, String str)
//		Hellocworldjava.lang.Object@7852e922
		sb.insert(11, "Student");
		System.out.println(sb);
		
//		StringBuffer deleteCharAt(int index) 
		sb.deleteCharAt(5);
		System.out.println(sb);
//		StringBuffer delete(int start, int end) 
		sb.delete(5, 10);
		System.out.println(sb);
		
//		public StringBuffer replace(int start,int end,String str)
		sb.replace(5, 12, "World");
		System.out.println(sb);
		
//		public StringBuffer reverse() 
//		sb.reverse();
//		System.out.println(sb);
		
//		public String substring(int start);
		String result = sb.substring(10);
		System.out.println(sb);
		System.out.println(result);
//		public String substring(int start,int end)
		System.out.println("sb:" + sb);
		String result2 = sb.substring(0, 5);
		System.out.println(result2);
		System.out.println(sb);				
	}
}

3.StringBuffer:

与StringBuffer基本类似,下面通过比较三者的关系来总结:

  • String,StringBuffer,StringBuilder三者的区别。
    String的特点: 线程不安全, 一旦在方法区创建就不会被更改,可以给多个引用共享,在做大量字符串拼接的时候效率低
    StringBuffer,StringBuilder 可以改变变字符串的长度和内容,是一个字符串缓冲区,在做大量字符串拼接的时候不会开辟新的空间

  • StringBuffer,StringBuilder的区别
    StringBuffer线程安全的,效率低
    StringBuilder线程不安全的,效率高

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值