1. 程式人生 > 實用技巧 >通過json 響應新增js function 增強業務靈活程度

通過json 響應新增js function 增強業務靈活程度

1. 字串常量

package com.qf.demo01string;


public class Test1String {

	public static void main(String[] args) {
		//1.字串常量:雙引號引起來的字串的內容
		//2.直接宣告一個字串:字串池中。相同內容的字串,就一份。共享。
		String s1 = "abc";//池中
		System.out.println(s1);
		
		String s2 = "abc";
		System.out.println(s2);
		
		System.out.println(s1 == s2); //true
		
		s2 = "memeda";
		
		String s3 = "abc";//池中
		
		System.out.println("-----------------------------");
		//2.通過new關鍵字,建立String物件。
		String s4 = new String("abc");//因為有new,在堆中建立一個物件,儲存在堆中
		
		String s5 = new String("abc");//因為有new,重新建立物件,儲存在堆中
		
		String s6 = new String("haha");//因為有new,重新建立物件,
		
		//地址的比較:==
		System.out.println(s1 == s3);//true
		System.out.println(s4 == s5);//false
		System.out.println(s1 == s4);//false
		
		//比較內容:equals,String類重寫了equals,專門比較內容
		System.out.println(s1.equals(s3));//true
		System.out.println(s4.equals(s5));//true
		System.out.println(s1.equals(s4));//true
			
	}
}

2. 字串的建立

package com.qf.demo01string;

public class Test2CreateString {

	public static void main(String[] args) {
		//1.null和""的區別:

		String s1 = new String();//字串物件存在,只是儲存的字元沒有,內容是空的。
		System.out.println(s1);
		
		String s3 = "";//""
		System.out.println(s3);//""
		
		
		String s2 = null;//字串物件不存在,直接訪問屬性或方法,會空指標異常。
		System.out.println(s2);
		
		System.out.println(s1.length());//該字串的長度
//		System.out.println(s2.length());//java.lang.NullPointerException
		
		
		//2.使用位元組陣列構建一個字串,IO流
		/*
		 * String(byte[] bytes, int offset, int length) 
			通過使用平臺的預設字符集解碼指定的位元組子陣列來構造新的 String 
			
			第一個引數:byte[] bytes,資料來源
			第二個引數:int offset,偏移量,從哪個下標開始的資料,構建字串
			第三個引數:int length,長度,獲取的個數。
		 */
		byte[] b1 = {65,66,67,68,69};//ABCDE
		String s4 = new String(b1);//使用b1這個陣列中的資料,構建一個字串
		System.out.println(s4);
		
		String s5 = new String(b1, 2, 3);
		System.out.println(s5);
		
		//3.通過字元陣列,構建一個字串
		char[] c1 = {'a','b','c','d','e','f'};
		String s6 = new String(c1);
		System.out.println(s6);
		
		String s7 = new String(c1, 1, 3);
		System.out.println(s7);
		
		
	}

} 

3. String常用方法

package com.qf.demo01string;

import java.util.Arrays;

public class Test3StringMethod {

	public static void main(String[] args) {
		String s1 = "helloWorld";//10個字元
		
		/*
		 * charAt(index)-->char,根據指定的下標獲取對應的字元。要注意越界問題。StringIndexOutOfBoundsException。
		 * index的取值:從0開始,到長度減1。
		 */
		char c = s1.charAt(0);
		System.out.println(c);
		
		/*
		 * concat(String)-->String,字串的拼接。效果同+起連線符作用一樣的。
		 返回一個拼接之後的新的字串
		 */
		String s2 = s1.concat("***");
		System.out.println(s2);
		
		
		/*
		 * contains()-->boolean判斷字串中,是否包含了指定的內容。
		 返回值是boolean,true,false。
		 */
		boolean b1 = s2.contains("***");
		System.out.println(b1);
		
		
		/*
		 * endsWith(String)--->boolean,判斷指定的字串,是否以某個字尾結尾。。
		 * startsWith(String)--->boolean,判斷是否以指定的內容開頭。
		 */
		String s3 = "aa.jpeg";
		if(s3.endsWith(".jpeg")){
			System.out.println(s3+",是一張圖片。。");
		}
		
		String s4 = "20200120記錄.txt";
		if(s4.startsWith("202001")){
			System.out.println("是今年1月份的記錄檔案。。");
		}
		
		/*
		 * equals(Object obj)-->boolean,比較兩個字串的內容是否相等。""引起來的內容是否一致
		 * equalsIgnoreCase()-->boolean,比較兩個字串的內容, 忽略大小寫。
		 */
		String s5 = "hello";
		String s6 = "HeLLo";
		System.out.println(s5.equals(s6));//Object類,此處是重寫:比較內容
		System.out.println(s5.equalsIgnoreCase(s6));
		
		
		/*
		 * getBytes()-->byte[]根據字串,獲取對應的位元組陣列。
		 * toCharArray()-->char[] 
		 */
		//s1 = "helloWorld";//10個字元
		byte[] bytes = s1.getBytes();
		System.out.println(Arrays.toString(bytes));
		
		char[] array = s1.toCharArray();
		System.out.println(Arrays.toString(array));
		
		
		/*
		 * indexOf(int 字元)-->index,在字串中,查詢指定的引數的字元,
		 返回值是該字元在字串中第一次出現的下標。如果沒有該字元,返回-1。
		 * 
		 * indexOf(String 子串)-->index,同上
		 * 
		 * indexOf(int 字元,int fromIndex)-->index,表示在字串中,
		 從fromIndex下標開始向後找指定的字元,如果有就返回下標,如果沒有就-1。
		 * 
		 * indexOf(String 子串,int fromIndex)-->同上
		 * 
		 * lastIndexOf(int 字元)-->index,在指定的字串中,搜尋該字元,
		 最後一次出現的位置。理解為倒著搜。
		 * 
		 * lastIndexOf(String 子串),同上
		 * 
		 * lastIndexOf(int 字元,int fromIndex)-->index,在字串中查詢指定的內容,
		 從fromIndex,從後往前,倒著搜,第一次出現的位置。
		 * 
		 * lastIndexOf(String ,int fromIndex)
		 */
		//s1 = "helloWorld";//10個字元
		int i1 = s1.indexOf('x');//
		System.out.println(i1);
		int i2 = s1.indexOf("llo");
		System.out.println(i2);
		int i3 = s1.indexOf('W',5);
		System.out.println(i3);
		
		
		int i4 = s1.lastIndexOf('l');
		System.out.println(i4);//8
		
		int i5 = s1.lastIndexOf('l', 0);
		System.out.println(i5);//3
		
		
		/*
		 * length()-->int,字串的長度,字串中字元的個數。
		 * 同陣列區分:
		 * 	length:屬性
		 * 	
		 */
		System.out.println(s1.length());//10
		System.out.println("abc".length());//3
		
		
		
		/*
		 * replace(oldchar , newchar)-->String,替換指定的字元,獲取新串
		 * replace(CharSequence,CharSequence)-->
		 */
		
		//s1 = "helloWorld";//10個字元
		String s7 = s1.replace('l', '*');
		System.out.println(s1);
		System.out.println(s7);
		
		String s8 = s1.replace("llo", "X");
		System.out.println(s1);
		System.out.println(s8);
		
		
		/*
		 * split(分隔符)-->String[] ,按照指定的內容,
		 將字串進行分離(切割),得到一個數組。
		 * 
		 * 注意點:分隔符,在開頭,在中間,都起作用的。在末尾不起作用。
		 */
		String s9 = "鵝鵝a鵝,曲項,,向a天歌,拔毛加漂水,點火蓋上鍋,,,,,,,,,,";//""
		System.out.println(s9);
		
		String[] arr = s9.split(",");
		System.out.println("--->"+arr.length);
		for(int i =0;i<arr.length;i++){
			System.out.println(arr[i]);
		}
		
		
		/*
		 * 字串的擷取
		 * substring(beginIndex)-->String,從引數表示的下標開始,擷取到末尾,
		 * substring(beginIndex,endIndex)-->String,
		 * 		[begin,end)
		 */
		//s1 = "helloWorld";//10個字元
		String s10 = s1.substring(5); ///World
		System.out.println(s1);
		System.out.println(s10);
		
		String s11 = s1.substring(2, 6);//lloW,包含下標2,不包含6之間的字元
		System.out.println(s11);
		
		/*
		 * 轉換大小寫
		 * toLowerCase(),
		 * toUpperCase(),
		 * 
		 */
		String s12  ="aBcD123**";
		System.out.println(s12.toLowerCase());//abcd123**
		System.out.println(s12.toUpperCase());//ABCD123**
		
		
		/*
		 * trim()--》String,去除首尾的空格
		 */
		
		String s13 = "  hello world ";//前2個空格,中間1個,末尾1個
		System.out.println(s13.length());
		String s14 = s13.trim();
		System.out.println(s14.length());
		
	}

}