在java編程中,字符串類是用得非常多的,我列舉了幾個常用的方法:
package com.newer.tw;import java.io.UnsupportedEncodingException;import java.lang.*;public class Stringcaozuo1 {	public static void main(String[] args) throws UnsupportedEncodingException	{		//將字節數組轉換成字符串		byte[] b={97,98,99,100};		String s=new String(b);		System.out.PRintln(s);//abcd				//將字節數組轉換成字符串2		byte[] b1={97,98,99,100,-28,-30};		String s1=new String(b1);		System.out.println(s1);//abcd溻				//將字節數組轉換成字符串2		byte[] b2={97,98,99,100,-28,-30};		String s2=new String(b2,"UTF-8");//通過使用指定的 charset 解碼指定的 byte 數組,構造一個新的 String。		System.out.println(s2);//abcd?				//		char[] c={'a','b','c','d'};		//String c1=new String(c);//分配一個新的 String,使其表示字符數組參數中當前包含的字符序列。		//System.out.println(c1);//abcd		String ss = new String(c,1,2);		System.out.println(ss);//bc				// 將字符串轉成字節數組		String s3 = "湖南長沙";		byte[] bs = s3.getBytes("UTF-8");		for (int i = 0; i < bs.length; i++) {				System.out.println(bs[i]);//-26 -71 -106 -27 -115 -105 -23 -107 -65 -26 -78 -103			}		// 獲得字符串中的指定字符		char c2 = s3.charAt(0);		System.out.println(c2);//湖		// 將字符串轉成字符數組		char[] cc = s3.toCharArray();		for (int i = 0; i < cc.length; i++) {				System.out.println(cc[i]);			}	}}
新聞熱點
疑難解答