【LeetCode刷題筆記(六十三)】之 226. 翻轉二叉樹
阿新 • • 發佈:2020-12-11
獲取類功能
public class test0 { public static void main(String[] args) { String str = "anAdEfg"; System.out.println("String類的獲取功能"); //獲取字串的長度 int length = str.length(); //獲取指定索引位置的字元 char c1 = str.charAt(0); //返回指定字元在此字串中第一次出現處的索引int c2 = str.indexOf('n'); //返回指定字串在此字串中第一次出現的索引 int c3 = str.indexOf("fg"); //返回指定字元在此字串中從指定位置後第一次出現處的索引。 int c4= str.indexOf('f', 2); //返回指定字串在此字串中從指定位置後第一次出現處的索引。 int c5 = str.indexOf("fg", 2); //從指定位置開始擷取字串,預設到末尾String c6 = str.substring(2); //從指定位置開始到指定位置結束擷取字串 String c7 = str.substring(2, 4); //注意4是位置不是長度 System.out.println(length); System.out.println(c1); System.out.println(c2); System.out.println(c3); System.out.println(c4); System.out.println(c5); System.out.println(c6); System.out.println(c7); } }
判斷類
public class test0 { public static void main(String[] args) { String str1 = "axcde"; String str2 = "Axcde"; System.out.println("String類的判斷功能"); //比較字串的內容是否相同,區分大小寫 boolean b = str1.equals(str2); //比較字串的內容是否相同,忽略大小寫 boolean b1 = str1.equalsIgnoreCase(str2); //判斷字串中是否包含傳遞進來的字串 boolean b2 = str1.contains("cde"); //判斷字串是否以傳遞進來的字串開頭 boolean b3 = str1.startsWith("ax"); //判斷字串是否以傳遞進來的字元出結尾 boolean b4 = str2.endsWith("de"); //判斷字串的內容是否為空 boolean b5 = str1.isEmpty(); System.out.println(b1); System.out.println(b2); System.out.println(b3); System.out.println(b4); System.out.println(b5); } }
等於判斷:
不支援[]操作符
常見string轉換
public class test0 { public static void main(String[] args) { String str = "anAdEfg"; String str1 = "axcde"; int a=123323; //把字串轉換為位元組陣列 byte[] bytes = str.getBytes(); for (int i = 0; i < bytes.length; i++) { System.out.print(bytes[i]+" "); } System.out.println(); //把字串轉換為字元陣列 char[] chars = str.toCharArray(); for (int i = 0; i < chars.length; i++) { System.out.print(chars[i]+" "); } System.out.println(); //把字元陣列轉換成字串 String s2 = new String (chars); System.out.println(s2); //把i.nt型別的資料轉成字串 String s3 = Integer.toString(a); System.out.println(s3); //把字串轉換成小寫 String s = str.toLowerCase(); System.out.println(s); //把字串變成大寫 String s1 = str.toUpperCase(); System.out.println(s1); //字串拼接 String s4 = str.concat(str1); System.out.println(s4); } }
public class test0 { public static void main(String[] args) { String str = " anAdEfg "; @SuppressWarnings("unused") String str1 = "axcde"; //將指定字元進行互換 String s = str.replace('a', 'b'); System.out.println(s); //將指定字串進行互換 String s1 = str.replace("an", "qq"); System.out.println(s1); //去除兩端空格 String s2 = str.trim(); System.out.println(s2); //通過字典順序去比較 返回的值是 ASCII 碼的差值 呼叫者減去傳入者 int i = "BCc".compareTo("ABc"); System.out.println(i); //如果前面幾個字母一樣會根據兩個字串長度進行減法運算返回該減法的結果 (通過長度去比) int i1 = "abc".compareTo("a"); System.out.println(i1); //忽略大小寫的比較 int i2 = "abc".compareToIgnoreCase("ABC"); System.out.println(i2); } }
string 和int 轉換
public class test0 { public static void main(String[] args) { //i/n/t->String int i = 1234567; String s = ""; s = i + "";//會產生兩個String物件 System.out.println(s); String s1 = String.valueOf(i);//使用String類的靜態方法,只產生一個String物件 System.out.println(s1); String s2 = Integer.toString(i); System.out.println(s2); //String->i/n/t String str = "12345"; int i1 = Integer.parseInt(str);//直接使用靜態方法,不會產生多餘的物件 System.out.println(i1); int i2 = Integer.valueOf(str).intValue(); System.out.println(i2); } }
string.equal 和== 的不同
public class test0 { public static void main(String[] args) { String s1 = new String("hello"); String s2 = new String("hello"); System.out.println(s1.equals(s2));//判斷字串內容 System.out.println(s1 == s2);//判斷字串引用 } }
字串排序
public class test0 { public static void main(String[] args) { String sortStr = "ACDFE"; char[] arrayCh = sortStr.toCharArray(); //1,把sortStr轉換為字元陣列 Arrays.sort(arrayCh);//2,利用陣列幫助類自動排序 System.out.println(Arrays.toString(arrayCh));//3,輸出 } }