Java——String类

String 字符串

  • 不可变长字符序列 “abc” “abcd”
  • 构造器
  • 方法
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class StringDemo01 {
	public static void main(String[] args) throws UnsupportedEncodingException {
		//String()
		String str1=new String();  //1个  new
		System.out.println(str1);
		
		//String(String original) 
		String str2=new String("abc");  //2个  new 	"abc"-字符串常量池中
		System.out.println(str2);
		
		//String(char[] value) 使用字符数组中所有字符构建一个字符串
		char[] arr= {'x','y','s','h'};
		String str3=new String(arr);
		System.out.println(str3);
		
		//String(char[] value, int offset, int count) 
		String str4=new String(arr,1,2);
		System.out.println(str4);
		
		//String(byte[] bytes, String charsetName) 
		byte[] bytes="你好就好,要过得比我好".getBytes("gbk");
		System.out.println(Arrays.toString(bytes));
		
		//String str5=new String(bytes);
		String str5=new String(bytes,"gbk");
		System.out.println(str5);
	}
}

常用方法

String str=“abcdefbc”;

// char charAt(int index)

	System.out.println(str.charAt(0));

//int codePointAt(int index)

	System.out.println(str.codePointAt(0));

//String concat(String str) 将指定字符串连接到此字符串的结尾。

	System.out.println(str.concat("哈哈"));

//boolean contains(CharSequence s)

	System.out.println(str.contains("bc"));

// boolean startsWith(String prefix)

System.out.println(str.startsWith("a"));

//byte[] getBytes()
//byte[] getBytes(String charsetName)
// int indexOf(String str)找到参数子串第一次出现的索引
// int lastIndexOf(String str)找到参数子串最后一次出现的索引

	System.out.println(str.indexOf("bc"));
	System.out.println(str.lastIndexOf("bc"));

//int length() 字符串中字符个数

	System.out.println(str.length());

// String replace(CharSequence target, CharSequence replacement)

	System.out.println(str.replace('b', 'B'));
	
	System.out.println(str.replace("bc", "BC"));

//String[] split(String regex)

	System.out.println(Arrays.toString(str.split("b")));

//String substring(int beginIndex) 从指定索引位置到最后

	System.out.println(str.substring(2));

//String substring(int beginIndex, int endIndex) 结束索引位置不包含

	System.out.println(str.substring(1, 3));

// char[] toCharArray() 字符串转为 字符数组

System.out.println(Arrays.toString(str.toCharArray()));

//String toLowerCase()
//String toUpperCase()

System.out.println(str.toUpperCase());

// String trim() 去空格
//static String valueOf(参数) 参数转字符串

	System.out.println(String.valueOf(false));

StringBuffer & StringBuilder

StringBuffer : 线程安全的可变长的字符序列

  • StringBuilder : 线程不安全的可变长字符序列
  • 一般线程安全的效率较低
  • String 不可变长的字符序列
  • 总结:
  • String 适合用于少量的字符串操作情况
  • StringBuilder 适用于单线程下字符穿冲去进行大量操作的情况
  • StringBuffer 使用多线程下字符缓冲区进行大量 操作的情况
public class StringDemo03 {
	public static void main(String[] args) {
		StringBuilder sb = new StringBuilder();  //默认初始容量为16大小的字节 数组
		System.out.println(sb);
		System.out.println(sb.length());
		System.out.println(sb.capacity());
		
		//StringBuilder(int capacity) 
		StringBuilder sb2 = new StringBuilder(10); 
		System.out.println(sb2.capacity());
		
		//StringBuilder(String str) 
		StringBuilder sb3 = new StringBuilder("abc"); 
		System.out.println(sb3);
		System.out.println(sb3.length());
		System.out.println(sb3.capacity());
		
		//StringBuilder append(boolean b)  追加
		System.out.println(sb.append(false));
		System.out.println(sb);
		
		//StringBuilder delete(int start, int end) 结束索引不包含
		System.out.println(sb.delete(1, 2));;
		System.out.println(sb);
		
		// StringBuilder insert(int offset, boolean b) 
		System.out.println(sb.insert(1, 'a')); 
		
		// StringBuilder reverse()  翻转
		sb.reverse();
		System.out.println(sb);
		//如果一个字符串要实现内容翻转,可以转为StringBuilder..对象使用reverse方法
	}
}

下一篇,日期类

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值