字串與字元

 文字字串是一個相當基本且經常被使用到的資料型態,然而在   Java   中字串不象   char、int   與   float   一樣是個基本資料型態,而是使用   java.lang.String   類別來加以表示,該類別定義了許多有用的方法來操作字串。String   物件是固定不變的(immutable):一旦一個   String   物件被建立了,則沒有任何方法可以改變它所代表的文字,因此,每個運作字串的方法會傳回一個新的   String   物件,而所修正過後的字串便是儲存在此新物件裏。  
   
  以下的程式碼展示了你可以對字串所執行的運作:  
   
  //   建立字串  
  String   s   =   "Now"; //   String   物件有個特殊的寫法  
  String   t   =   s   +   "   is   the   time."; //   使用   +   運算子來串連字串  
  String   t1   =   s   +   "   "   +   23.4; //   +   將其它值轉換爲字串  
  t1   =   String.valueOf('c'); //   從字元值獲得對應的字串  
  t1   =   String.valueOf(42); //   獲得整數或其他任何數值的字串版本  
  t1   =   Object.toString(); //   使用   toString()   將物件轉換爲字串  
   
  //   字串長度  
  int   len   =   t.length(); //   字串中的字元數:16  
   
  //   字串中的子字串  
  String   sub   =   t.substring(4); //   傳回從   char   4   到最後的子字串:"is   the   time."  
  sub   =   t.substring(4,   6); //   傳回   chars   4   與   5:"is"  
  sub   =   t.substring(0,   3); //   傳回   chars   0   到   2:"Now"  
  sub   =   t.substring(x,   y); //   傳回從位置   x   到   y-1   間的子字串  
  int   numchars   =   sub.length(); //   子字串的長度永遠是   (y-x)  
   
  //   從一個字串中擷取(extract)出字元  
  char   c   =   t.charAt(2); //   取得   t   的第三個字元:w  
  char[]   ca   =   t.toCharArray(); //   將字串轉換爲一個字元陣列  
  t.getChars(0,   3,   ca,   1); //   將   t   中的前三個字元放到   ca[1]   到   ca[3]   中  
   
  //   大小寫轉換  
  String   caps   =   t.toUpperCase(); //   轉換爲大寫  
  String   lower   =   t.toLowerCase(); //   轉換爲小寫  
   
  //   字串比較  
  boolean   b1   =   t.equals("hello"); //   傳回   flase:兩字串並不相等  
  boolean   b2   =   t.equalsIgnoreCase(caps); //   忽略大小寫的字串比較:true  
  boolean   b3   =   t.startsWith("Now"); //   傳回   true  
  boolean   b4   =   t.endsWith("time."); //   傳回   true  
  int   r1   =   s.compareTo("Pow"); //   傳回值   <0:s   在"Pow"之前  
  int   r2   =   s.compareTo("Now"); //   傳回值   0:兩字串相等  
  int   r3   =   s.compareTo("Mow"); //   傳回值   >0:s   在"Mow"之後  
  r1   =   s.compareToIgnoreCase("pow"); //   傳回值   <0(Java   1.2   之後才支援)  
   
  //   搜尋字元與子字串  
  int   pos   =   t.indexOf('i'); //   'i'   的位置:4  
  pos   =   t.indexOf('i',   pos   +   1); //   下一個   'i'   的位置:12  
  pos   =   t.indexOf('i',   pos   +   1); //   字串中已經沒有   'i'   了,傳回   -1  
  pos   =   t.lastIndexOf('i'); //   字串中最後一個   'i'   的位置:12  
  pos   =   t.lastIndexOf('i',   pos   -   1); //   從   char   11   開始往前搜尋   'i'  
   
  pos   =   t.indexOf("is"); //   搜尋子字串:傳回   4  
  pos   =   t.indexOf("is",   pos   +   1); //   只出現一次:傳回   -1  
  pos   =   t.lastIndexOf("the"); //   由後往前搜尋字串  
  String   noun   =   t.substring(pos   +   4); //   擷取出   "the"   之後的字串  
   
  //   將某個字元實體轉換爲另一個字元  
  String   exclaim   =   t.replace('.',   '!'); //   只能與字元一起使用,不能與子字串一起使用  
   
  //   將字串最前面的與最後面的空白刪除  
  String   noextraspaces   =   t.trim();  
   
  //   使用   intern()   獲得獨有的字串實體  
  String   s1   =   s.intern(); //   傳回   s1,相等於   s  
  String   s2   =   "Now".intern(); //   傳回   s2,相等於   "Now"  
  boolean   equals   =   (s1   ==   s2); //   以   ==   測試   s1、s2   是否相等  

 

 

 

 

String temp 
=
 
"
woaizhonghua
"
;
String temp1
= temp.replaceAll( " (wo|ai|zhong|hua) " , " $1, " );
System.out.println(temp1.substring(
0 ,temp1.length() - 1 ));
發佈了26 篇原創文章 · 獲贊 6 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章