Java中的常用类:String

常用类 String :

  • String
    • 不可变长的字符序列 ,Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。
  • StringBuilder
    • 可变长的字符序列 ,线程不安全,效率高
  • StringBuffer
    • 可变长的字符序列 , 线程安全的,效率较低
public class StringDemo02 {
	public static void main(String[] args) throws UnsupportedEncodingException {
		//空串
		String str1=new String();
		System.out.println(str1.length());
		//字符数组转为字符串
		//static String copyValueOf(char[] data) 
		//String(char[] value)    分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
		char[] ch=new char[]{'a','b','c','d','e','f'};
		String str2=new String(ch);
		System.out.println(str2);
		
		//String(char[] value, int offset, int count)  分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
		String str3=new String(ch,2,3);
		System.out.println(str3);
		
		//String(String original) 
		String str4=new String("abc");
		System.out.println(str4);
		
		byte[] arr="因为你好".getBytes("gbk");
		//String(byte[] bytes)   平台默认
		String str5=new String(arr);
		System.out.println(str5);
		System.out.println(Arrays.toString(arr));
		
		//String(byte[] bytes, String charsetName)  通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
		String str6=new String(arr,"gbk");
		System.out.println(str6);
		
		//String(byte[] bytes, int offset, int length, String charsetName) 
		String str7=new String(arr,2,4,"gbk");
		System.out.println(str7);
		
		//char charAt(int index) 返回指定索引处的 char 值。 
		System.out.println(str6.charAt(2));
		
		// int compareTo(String anotherString)  按字典顺序比较两个字符串。 
		System.out.println("ab".compareTo("A")); //32
		
		// String concat(String str)将指定字符串连接到此字符串的结尾。 
		System.out.println(str6.concat("哈哈"));
		//boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。 
		System.out.println(str6.concat("哈哈"));
		
		// boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。
		System.out.println(str6.contains("你好"));
		
		//byte[] getBytes()  
		//byte[] getBytes(String charsetName)  
		System.out.println(str6.getBytes());
		
		//void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)  将字符从此字符串复制到目标字符数组。 
		char[] c=new char[5];
		str6.getChars(0, 4, c, 1);  //结束位置不包含
		System.out.println(c);
		
		//int indexOf(String str) 
		System.out.println("abcababc".indexOf('a'));
		System.out.println("abcababc".lastIndexOf('a'));
		
		//String replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 
		String s="  abca babc  ";
		System.out.println(s.replace('a', 'A'));
		
		// String[] split(String regex) 
		 String ss="name=zhangsan";
		 System.out.println(Arrays.toString(ss.split("=")));
		 
		 //String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。 
		 //String substring(int beginIndex, int endIndex)  endIndex结束位置索引不包含
		 System.out.println(s.substring(5));
		 System.out.println(s.substring(3,5));
		 //char[] toCharArray()   将此字符串转换为一个新的字符数组。 
		 System.out.println(s.toCharArray());
		 
		 //String toLowerCase() 
		 // String trim() 返回字符串的副本,忽略前导空白和尾部空白 
		 System.out.println(s.trim());
		 
		 //valueOf(参数) 任意类型参数转为字符串

	}
}
public class StringDemo03 {
	public static void main(String[] args) {
		//StringBuilder(String str)
		StringBuilder sb=new StringBuilder("haha");  //默认16大小
		System.out.println(sb);
		//int capacity() 
		 // length()
		System.out.println(sb.capacity());
		System.out.println(sb.length());
		
		//重点方法:
		//StringBuilder append(int i)   追加
		StringBuilder s=sb.append(true);
		System.out.println(sb);
		System.out.println(sb==s);
		
		// StringBuilder delete(int start, int end)   end不包含 删除
		System.out.println(sb.delete(4, 6));
		
		//StringBuilder insert(int offset, char c)   插入
		System.out.println(sb.insert(3, 123));
		
		 //StringBuilder reverse()  翻转
		System.out.println(sb.reverse());
		
		String str="abcdef";
		char[] arr=str.toCharArray();
		System.out.println(Arrays.toString(arr));
		String str2="";
		for(int i=arr.length-1;i>=0;i--){
			str2+=arr[i];
		}
		System.out.println(str2);
		
		String str3=new String(sb);
		System.out.println(str3);
		
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值