
1 2 3 4 5 6 7 8 9 10 11 12 | public class CH012 { public static void main(String args[]) throws IOException { String str1= "hello" ; String str2= new String( "Kitty!" ); System.out.println(str1+ " " +str2); } } |
字元與字串
前面瞭解多個字元合併成為字串,例如"Kitty"
字串是由'K'、'i'、't'、't'、'y'
五個字元所組成的字元陣列。可用用「length」得到字元數量,用「substring」、「charAt」、「index等方法找出固定位置。1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class CH013 { public static void main(String args[]) { String str = "hello Kitty!" ; System.out.println(str.length()); //字串中的字元數量 System.out.println(str.charAt( 3 )); //字串中第4個字元 System.out.println(str.substring( 4 , 7 )); //字串中第5到7個字元 System.out.println(str.indexOf( "o" )); //字串中的「o」在第幾個位置 } } |
字元與字串的關係密切,但字元不同於字串。
字串搜尋與取代
「replace(要被取代的字串,要取代的字串)」取代全部找到的「要被取代字串」, 「replaceFirst (要被取代的字串,要取代的字串)」只取代第一個找到的「要被取代字串」。 其中replaceAll函數可以使用正規表示式來做整批有規則性的取代1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class CH014 { public static void main(String args[]) { String str = "hello Kitty!" ; System.out.println(str.replace( "Kitty!" , "小梅子" )); //置換的結果不會改變原字串 System.out.println(str); //看看是不是真的 str=str.replace( "Kitty!" , "everyone" ); //要明確賦予新值 System.out.println(str); //才會變成新字串 } } |
方法名稱後方的小括號代表函數,會有單獨章節介紹。
字串連結
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class CH015 { public static void main(String args[]) { String s1= "1" ; String s2= "2" ; int s3= 3 ; System.out.println( s1 + s2 + s3 + 3 + 3 ); } } |
字串可以用「 + 」連結,讓多個字串合併成一個字串。
也可以隨機與「 " "」雙引號夾住的即時字串連結。
任何類型資料與字串相加後,結果都會變成字串。
字串比較
字串是一種特別的資料型態,它是一個物件類別卻又屬於基本型態。能像基本資料型態直接用「 = 」賦值,也可以用建立物件的方式「 new 」來產生。 「 equals 」是所有物件提供的基本方法,可以比較二個物件的值是否相同,「 = 」則是比較物件(實體、記憶體空間)是否相同。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class CH016 { public static void main(String args[]) { String s4 = new String( "restful" ) ; String s5 = new String( "restful" ) ; String s6 = new String( "peaceful" ) ; String s7 = s4 ; String s8= "restful" ; String s9= "restful" ; System.out.println(s4.equals(s5)) ; System.out.println(s4.equals(s6)) ; System.out.println(s4 == s5) ; System.out.println(s4 == s7) ; System.out.println(s4 == s8) ; System.out.println(s8 == s9) ; } } |
System.out.println(s4.equals(s5));//s4和s5所帶的值相同---true
System.out.println(s4.equals(s6));//s4和s6所帶的值不相同----false
System.out.println(s4 == s5); //s4和s5各new了不同的記憶體空間----false
System.out.println(s4 == s7); //因為把s7指向了和s4所指向的同一個空間----true
System.out.println(s4 == s8); //因為s4 new了一個新空間,而s8則在字串池中---false
System.out.println(s8 == s9); //s8和s9都在字串池中,為節省記憶體,會指向同一個記憶體空間-----true
字串轉數值
鍵盤、滑鼠、網路線、USB線,各種I/O裝置傳入值都是串流。通常都需要讀取串流解析為字串,文字資料必需變成為字串才能進行各項後續處理。而字串又要轉換為不同的資料型態,才能進行各項運算。各種型態的數值都有轉換方式,Integer.ParseInt(String)、Float.ParseFloat等。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CH0506 { public static void main(String args[]) throws IOException { BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); System.out.print( "請輸入一個數字: " ); int sum = Integer.parseInt(buf.readLine()); //Integer.ParseInt(String)方法將字串轉為int數字型態 System.out.print( "請再輸入一個數字: " ); //System.out.println(sum+"+" + buf.readLine()+" = "+(sum + Integer.parseInt(buf.readLine()))); System.out.println( "加總為" +(sum + Integer.parseInt(buf.readLine()))); } } |
字串轉型可能會發生預期外的事件,例如使用者輸入的不是數字鍵、數字太大太小、空白或沒輸入等X事。雖然Java有完善的錯誤處理機制,不過那是之後的事。現在我們先利用IDE提供的throws自動丟出例外throws IOException來敷衍編譯器。因為編譯器認定有輸入輸出的動作,就要有錯誤處理機制。放throws 的意思是交給呼叫的類別來處理,main是主類別當main用throws出錯時仍然會使程式中斷。
數值轉字串
任何東西都能轉字串,物件、數值都可以轉為字串。轉字串的意義不大,數值與字串用「+」連接就算不轉字串也會被溶合為字串,但是總有用到需要字串的時候。物件轉字串有基本方法toString(),而數值轉字串要用String.values(數值)。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class CH0507 { public static void main(String args[]) { int num= 1 ; String str=String.valueOf(num); System.out.println(str); Float n= 0 .1f; str=n.toString(); System.out.println(str); } } |
數值或物件轉字串不會發生預期外的事件,無論再離譜的東西都能轉成字串。連空值也會轉出「null」4個字,所以不需要錯誤處理機制。
1 | String str=String.valueOf( 100 ); |
沒有留言:
張貼留言