》JavaAPI 文檔註釋可以在:類,常量,方法上聲明 文檔註釋可以被javadoc命令所解析並且根據內容生成手冊 1 package cn.fury.se_day01; 2 /** 3 * 文檔註釋可以在:類,常量,方法上聲明 4 * 文檔註釋可以被javadoc命令所解析並且根據內容生成手冊 5 ...
》JavaAPI
文檔註釋可以在:類,常量,方法上聲明
文檔註釋可以被javadoc命令所解析並且根據內容生成手冊
1 package cn.fury.se_day01; 2 /** 3 * 文檔註釋可以在:類,常量,方法上聲明 4 * 文檔註釋可以被javadoc命令所解析並且根據內容生成手冊 5 * 這個類用來測試文檔註釋 6 * @author soft01 7 * @version 1.0 8 * @see java.lang.String 9 * @since jdk1.0 10 * 11 */ 12 public class APIDemo { 13 public static void main(String[] args){ 14 System.out.println(sayHello("fury")); 15 } 16 /** 17 * 問候語,在sayHello中被使用 18 */ 19 public static final String INFO = "你好!"; 20 /** 21 * 將給定的用戶名上添加問候語 22 * @param name 給定的用戶名 23 * @return 帶有問候語的字元串 24 */ 25 public static String sayHello(String name){ 26 return INFO+name; 27 } 28 }測試代碼
》字元串是不變對象:字元串對象一旦創建,內容就不可更改
》》字元串對象的重用
**要想改變內容一定會創建新對象**
TIP: 字元串若使用字面量形式創建對象,會重用以前創建過的內容相同的字元串對象。
重用常量池中的字元串對象:就是在創建一個字元串對象前,先要到常量池中檢查是否這個字元串對象之前已經創建過,如果是就會進行重用,如果否就會重新創建
1 package cn.fury.test; 2 3 public class Test{ 4 public static void main(String[] args) { 5 String s1 = "123fury"; //01 6 String s2 = s1; //02 7 String s3 = "123" + "fury"; //03 8 String s4 = "warrior"; 9 System.out.println(s1 == s2); 10 System.out.println(s3 == s1); 11 System.out.println(s4 == s1); 12 } 13 } 14 15 /** 16 * 01 以字面量的形式創建對象:會重用常量池中的字元串對象 17 * 02 賦值運算:是進行的地址操作,所以會重用常量池中的對象 18 * 03 這條語句編譯後是:String s3 = "123fury"; 19 */字元串對象的重用
1 package cn.fury.se_day01; 2 /** 3 * 字元串是不變對象:字元串對象一旦創建,內容是不可改變的, 4 * **要想改變內容一定會創建新對象。** 5 * 6 * 字元串若使用字面量形式創建對象,會重用以前創建過的內容相同的字元串對象。 7 * @author soft01 8 * 9 */ 10 public class StringDemo01 { 11 public static void main(String[] args) { 12 String s1 = "fury123"; 13 // 字面量賦值時會重用對象 14 String s2 = "fury123"; 15 // new創建對象時則不會重用對象 16 String s3 = new String("fury123"); 17 /* 18 * java編譯器有一個優化措施,就是: 19 * 若計算表達式運算符兩邊都是字面量,那麼編譯器在生成class文件時 20 * 就會將結果計算完畢並保存到編譯後的class文件中了。 21 * 22 * 所以下麵的代碼在class文件裡面就是: 23 * String s4 = "fury123"; 24 */ 25 String s4 = "fury" + "123"; //01 26 String s5 = "fury"; 27 String s6 = s5 + "123"; //02 28 String s7 = "fury"+123; //01 29 String s8 = "fu"+"r"+"y"+12+"3"; 30 31 String s9 = "123fury"; 32 String s10 = 12+3+"fury"; //編譯後是String s10 = "15fury"; 33 String s11 = '1'+2+'3'+"fury"; //03 34 String s12 = "1"+2+"3"+"fury"; 35 String s13 = 'a'+26+"fury"; //04 36 37 System.out.println(s1 == s2); //true 38 System.out.println(s1 == s3); //false 39 System.out.println(s1 == s4); //true 40 System.out.println(s1 == s6); //false 41 System.out.println(s1 == s7); //true 42 System.out.println(s1 == s8); //true 43 System.out.println(s9 == s10); //false 44 System.out.println(s9 == s11); //false 45 System.out.println(s9 == s12); //true 46 System.out.println(s9 == s13); //true 47 /* 48 * 用來驗證03的演算法 49 */ 50 int x1 = '1'; 51 System.out.println(x1); 52 System.out.println('1' + 1); 53 /* 54 * 用來驗證04的演算法 55 */ 56 int x2 = 'a'; 57 System.out.println(x2); 58 System.out.println('a' + 26); 59 // System.out.println(s10); 60 } 61 } 62 63 /* 64 * 01 編譯完成後:String s4 = "fury123"; 因此會重用對象 65 * 02 不是利用字面量形式創建對象,所以不會進行重用對象 66 * 03 '1'+2 的結果不是字元串形式的12,而是字元1所對應的編碼加上2後的值 67 * 04 'a'+26 的結果是字元a所對應的編碼值再加上26,即:123 68 */list
》》字元串長度
中文、英文字元都是按照一個長度進行計算
1 package cn.fury.se_day01; 2 /** 3 * int length() 4 * 該方法用來獲取當前字元串的字元數量, 5 * 無論中文還是英文每個字元都是1個長度 6 * @author soft01 7 * 8 */ 9 public class StringDemo02 { 10 public static void main(String[] args) { 11 String str = "hello fury你好Java"; 12 System.out.println(str.length()); 13 14 int [] x = new int[3]; 15 System.out.println(x.length); 16 } 17 }字元串的長度
》》子串出現的位置
1 public int indexOf(String str) { 2 return indexOf(str, 0); 3 } 4 5 /** 6 * Returns the index within this string of the first occurrence of the 7 * specified substring, starting at the specified index. 8 * 9 * <p>The returned index is the smallest value <i>k</i> for which: 10 * <blockquote><pre> 11 * <i>k</i> >= fromIndex {@code &&} this.startsWith(str, <i>k</i>) 12 * </pre></blockquote> 13 * If no such value of <i>k</i> exists, then {@code -1} is returned. 14 * 15 * @param str the substring to search for. 16 * @param fromIndex the index from which to start the search. 17 * @return the index of the first occurrence of the specified substring, 18 * starting at the specified index, 19 * or {@code -1} if there is no such occurrence. 20 */ 21 public int indexOf(String str, int fromIndex) { 22 return indexOf(value, 0, value.length, 23 str.value, 0, str.value.length, fromIndex); 24 }方法解釋
1 package cn.fury.se_day01; 2 /** 3 * int indexOf(String str) 4 * 查看給定字元串在當前字元串中的位置 5 * 首先該方法會使用給定的字元串與當前字元串進行全匹配 6 * 當找到位置後,會將給定字元串中第一個字元在當前字元串中的位置返回; 7 * 沒有找到就返回 **-1** 8 * 常用來查找關鍵字使用 9 * @author soft01 10 * 11 */ 12 public class StringDemo03 { 13 public static void main(String[] args) { 14 /* 15 * java編程思想: Thinking in Java 16 */ 17 String str = "thinking in java"; 18 int index = str.indexOf("java"); 19 System.out.println(index); 20 index = str.indexOf("Java"); 21 System.out.println(index); 22 /* 23 * 重載方法: 24 * 從給定位置開始尋找第一次出現給定字元串的位置 25 */ 26 index = str.indexOf("in", 3); 27 System.out.println(index); 28 /* 29 * int lastIndexOf(String str) 30 * 返回給定的字元串在當前字元串中最後一次出現的位置 31 */ 32 int last = str.lastIndexOf("in"); 33 System.out.println(last); 34 } 35 }方法應用
》》截取部分字元串
String substring(int start, int end)
1 Open Declaration String java.lang.String.substring(int beginIndex, int endIndex) 2 3 4 Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. 5 6 Examples: 7 8 "hamburger".substring(4, 8) returns "urge" 9 "smiles".substring(1, 5) returns "mile" 10 11 Parameters:beginIndex the beginning index, inclusive.endIndex the ending index, exclusive.Returns:the specified substring.Throws:IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.方法解釋
1 package cn.fury.se_day01; 2 /** 3 * String substring(int start, int end) 4 * 截取當前字元串的部分內容 5 * 從start處開始,截取到end(但是不含有end對應的字元) 6 * 7 * java API有個特點,凡是使用兩個數字表示範圍時,通常都是“含頭不含尾” 8 * @author soft01 9 * 10 */ 11 public class StringDemo04 { 12 public static void main(String[] args) { 13 String str = "www.oracle.com"; 14 15 //截取oracle 16 String sub = str.substring(4, 10); 17 System.out.println(sub); 18 19 /* 20 * 重載方法,只需要傳入一個參數,從給定的位置開始連續截取到字元串的末尾 21 */ 22 sub = str.substring(4); 23 System.out.println(sub); 24 } 25 }方法應用
1 package cn.fury.test; 2 3 import java.util.Scanner; 4 5 /** 6 * 網址功能變數名稱截取 7 * @author Administrator 8 * 9 */ 10 public class Test{ 11 public static void main(String[] args) { 12 Scanner sc = new Scanner(System.in); 13 System.out.print("請輸入網址:"); 14 String s1 = sc.nextLine(); 15 int index1 = s1.indexOf("."); 16 int index2 = s1.indexOf(".", index1 + 1); 17 String s2 = s1.substring(index1 + 1, index2); 18 System.out.println("你輸入的網址是:"); 19 System.out.println(s1); 20 System.out.println("你輸入的網址的功能變數名稱為:"); 21 System.out.println(s2); 22 } 23 }實際應用
》》去除當前字元串中兩邊的空白
String trim()
去除當前字元串中兩邊的空白
1 Open Declaration String java.lang.String.trim() 2 3 4 Returns a string whose value is this string, with any leading and trailing whitespace removed. 5 6 If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u005Cu0020' (the space character), then a reference to this String object is returned. 7 8 Otherwise, if there is no character with a code greater than '\u005Cu0020' in the string, then a String object representing an empty string is returned. 9 10 Otherwise, let k be the index of the first character in the string whose code is greater than '\u005Cu0020', and let m be the index of the last character in the string whose code is greater than '\u005Cu0020'. A String object is returned, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m + 1). 11 12 This method may be used to trim whitespace (as defined above) from the beginning and end of a string. 13 Returns:A string whose value is this string, with any leading and trailing white space removed, or this string if it has no leading or trailing white space.方法解釋
1 package cn.fury.se_day01; 2 /** 3 * String trim() 4 * 去除當前字元串中兩邊的空白 5 * @author soft01 6 * 7 */ 8 public class StringDemo06 { 9 public static void main(String[] args) { 10 String str1 = " Keep Calm and Carry on. "; 11 System.out.println(str1); 12 String str2 = str1.trim(); //01 13 System.out.println(str2); 14 System.out.println(str1 == str2); //02 15 } 16 } 17 18 /* 19 * 01 改變了內容,因此創建了新對象,所以02的輸出結果為false 20 */方法應用
》》查看指定位置的字元
char charAt(int index)
返回當前字元串中給定位置處對應的字元
1 Open Declaration char java.lang.String.charAt(int index) 2 3 4 Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing. 5 6 If the char value specified by the index is a surrogate, the surrogate value is returned. 7 8 Specified by: charAt(...) in CharSequence 9 Parameters:index the index of the char value.Returns:the char value at the specified index of this string. The first char value is at index 0.Throws:IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.方法解釋
1 package cn.fury.se_day01; 2 /** 3 * char charAt(int index) 4 * 返回當前字元串中給定位置處對應的字元 5 * @author soft01 6 * 7 */ 8 public class StringDemo07 { 9 public static void main(String[] args) { 10 String str = "Thinking in Java"; 11 // 查看第10個字元是什麼? 12 char chr = str.charAt(9); 13 System.out.println(chr); 14 15 /* 16 * 檢查一個字元串是否為迴文? 17 */ 18 } 19 }方法應用
1 package cn.fury.test; 2 3 import java.util.Scanner; 4 5 public class Test{ 6 public static void main(String[] args) { 7 Scanner sc = new Scanner(System.in); 8 System.out.print("請輸入一個字元串(第三個字元必須是字元W):"); 9 while(true){ 10 String s1 = sc.nextLine(); 11 if(s1.charAt(2) == 'W'){ 12 System.out.println("輸入正確"); 13 break; 14 }else{ 15 System.out.print("輸入錯誤,請重新輸入。"); 16 } 17 } 18 } 19 }實際應用
1 package cn.fury.se_day01; 2 /* 3 * 判斷一個字元串是否是迴文數:個人覺得利用StringBuilder類中的反轉方法reverse()更加簡單 4 */ 5 public class StringDemo08 { 6 public static void main(String[] args) { 7 /* 8 * 上海的自來水來自海上 9 * 思路: 10 * 正數和倒數位置上的字元都是一致的,就是迴文 11 */ 12 String str = "上海自水來自海上"; 13 System.out.println(str); 14 // myMethod(str); 15 teacherMethod(str); 16 } 17 18 private static void teacherMethod(String str) { 19 for(int i = 0; i < str.length() / 2; i++){ 20 if(str.charAt(i) != str.charAt(str.length() - 1 - i)){ 21 System.out.println("不是迴文"); 22 /* 23 * 方法的返回值類型是void時,可以用return來結束函數 24 * return有兩個作用 25 * 1 結束方法 26 * 2 將結果返回 27 * 但是若方法返回值為void時,return也是可以單獨使用的, 28 * 用於結束方法 29 */ 30 return; 31 } 32 } 33 System.out.println("是迴文"); 34 } 35 36 private static void myMethod(String str) { 37 int j = str.length() - 1; 38 boolean judge = false; 39 for(int i = 0; i <= j; i++){ 40 if(str.charAt(i) == str.charAt(j)){ 41 judge = true; 42 j--; 43 }else{ 44 judge = false; 45 break; 46 } 47 } 48 if(judge){ 49 System.out.println("是迴文"); 50 }else 51 { 52 System.out.println("不是迴文"); 53 } 54 } 55 }實際應用2_迴文數的判斷
》》開始、結束字元串判斷
boolean startsWith(String star)
boolean endsWith(String str)
前者是用來判斷當前字元串是否是以給定的字元串開始的,
後者是用來判斷當前字元串是否是以給定的字元串結尾的。
1 package cn.fury.se_day01; 2 /** 3 * boolean startsWith(String star) 4 * boolean endsWith(String str) 5 * 前者是用來判斷當前字元串是否是以給定的字元串開始的, 6 * 後者是用來判斷當前字元串是否是以給定的字元串結尾的。 7 * @author soft01 8 * 9 */ 10 public class StringDemo09 { 11 public static void main(String[] args) { 12 String str = "thinking in java"; 13 System.out.println(str.startsWith("th")); 14 System.out.println(str.endsWith("va")); 15 } 16 }方法應用
》》大小寫轉換
String toUpperCase()
String toLowerCase()
作用:忽略大小寫
應用:驗證碼的輸入
1 package cn.fury.se_day01; 2 /** 3 * 將一個字元串中的英文部分轉換為全大寫或者全小寫 4 * 只對英文部分起作用 5 * String toUpperCase() 6 * String toLowerCase() 7 * 8 * 作用:忽略大小寫 9 * 應用:驗證碼的輸入 10 * @author soft01 11 * 12 */ 13 public class StringDemo10 { 14 public static void main(String[] args) { 15 String str = "Thinking in Java你好"; 16 System.out.println(str); 17 System.out.println(str.toUpperCase()); 18 System.out.println(str.toLowerCase()); 19 } 20 }方法應用
1 package cn.fury.test; 2 3 import java.util.Scanner; 4 5 public class Test{ 6 public static void main(String[] args) { 7 Scanner sc =