溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

String的構(gòu)造方法和一般方法

發(fā)布時間:2020-08-18 16:57:25 來源:網(wǎng)絡(luò) 閱讀:502 作者:航天嘎子 欄目:關(guān)系型數(shù)據(jù)庫
public class StringTest{
    public static void main(String[] args){
        //1
        String s1 = "abc";
        //2
        String s2 = new String("cdg");
        //3
        byte[] bytes={98,88,34,25};
        String s3 = new String(bytes);
        //4String(byte[] bytes, int offset, int length)Constructs a new String by decoding the specified subarray of bytes using the platform's default charse;
        String s4 = new String(bytes,1,3);
        //5.
	char[] c1 = {'我','是','中','國','人'};
	String s5 = new String(c1);
	System.out.println(s5); //我是中國人
		
	//6.
	String s6 = new String(c1,2,2);
	System.out.println(s6); //中國System.out.println(s3); //abcd  String已經(jīng)重寫了Object中的toString
    }
}

/*字符串常用方法
*/

public class StringTest06{
	
	public static void main(String[] args){
		
		//1.char charAt(int index);
		String s1 = "我是王勇,是壞人!";
		char c1 = s1.charAt(2);
		System.out.println(c1); //王
		
		//2.boolean endsWith(String endStr);
		System.out.println("HelloWorld.java".endsWith("java")); //true
		System.out.println("HelloWorld.java".endsWith(".java")); //true
		System.out.println("HelloWorld.java".endsWith("HelloWorld.java")); //true
		
		System.out.println("HelloWorld.java".endsWith("txt")); //false
		System.out.println("HelloWorld.java".endsWith("java ")); //false
		
		//3. boolean equalsIgnoreCase(String anotherString);
		System.out.println("abc".equalsIgnoreCase("ABc")); //true
		
		//4.byte[] getBytes();
		byte[] bytes = "abc".getBytes();
		
		for(int i=0;i<bytes.length;i++){
			System.out.println(bytes[i]);
		}
		
		//5.int indexOf(String str);
		System.out.println("http://192.168.1.100:8080/oa/login.action?username=jack&pwd=123".indexOf("/oa"));//25
		
		//6.int indexOf(String str, int fromIndex);
		System.out.println("javaoraclec++javavb".indexOf("java",1)); //13
		
		//7.int lastIndexOf(String str) 
		System.out.println("javaoraclec++javavb".lastIndexOf("java")); //13
		
		//8.int lastIndexOf(String str, int fromIndex) 
		System.out.println("javaoraclec++javavb".lastIndexOf("java",14)); //13
		
		//9.int length();
		System.out.println("abc".length()); //數(shù)組是length屬性,String是length()方法
		
		//10. String replaceAll(String s1,String s2);
		//mysqloraclec++mysqlvb
		System.out.println("javaoraclec++javavb".replaceAll("java","mysql")); //這個程序是4個字符串
		
		//11.String[] split(String s);
		String myTime = "2008,08,08";
		String[] ymd = myTime.split(",");
		
		for(int i=0;i<ymd.length;i++){
			System.out.println(ymd[i]);
		}
		
		//12.boolean startsWith(String s);
		System.out.println("/system/login.action".startsWith("/")); //true
		
		//13.String substring(int begin);
		System.out.println("/oa/login.action".substring(3)); //  /login.action
		
		//14. String substring(int beginIndex, int endIndex) 
		System.out.println("/oa/login.action".substring(4,9)); //login
		
		//15.char[] toCharArray();//字符串到字符數(shù)組的轉(zhuǎn)化
		char[] c2 = "我是李海波".toCharArray();
		for(int i=0;i<c2.length;i++){
			System.out.println(c2[i]);
		}
		
		//16.轉(zhuǎn)換成大寫
		System.out.println("Abcdef".toUpperCase());
		
		//17.轉(zhuǎn)換成小寫
		System.out.println("ABCDEf".toLowerCase());
		
		//18.String trim();、、去掉兩邊的空格
		System.out.print("       a  bcd e          ".trim());
		System.out.println("TEST");
		
		//19.String valueOf(Object obj); Returns the string representation of the Object argument.
		Object o = null;
		System.out.println(o); //不會,因?yàn)椴⒉皇侵苯诱{(diào)用toString方法,String.valueOf(Object)這個方法對空值進(jìn)行處理了。
		System.out.println(String.valueOf(o));
		//System.out.println(o.toString()); //會出現(xiàn)空指針


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI