String類和StringBuffer類的方法


一、String類的方法

  • public char charAt(int index)          返回字符串index個字符
  • public int length()         返回字符串長度
  • public int indexof(String str)        返回字符串中出現str的第一個位置
  • public int indexof(String str, int fromIndex)      返回字符串中從fromIndex開始出現str的第一個位置
  • public boolean equalsIngoreCase(String another)         比較字符串與another是否一樣(忽略大小寫)
  • public String replace(char oldChar, char newChar)         在字符串中用newChar字符替代oldChar字符,返回一個新的字符串
  • public String replaceAll(String regex,String replacement)使用給定的 replacement 字符串替換此字符串匹配給定的正則表達式的每個子字符串。
  • public boolean startsWith(String prefix)         判斷字符串是否以prefix字符串開頭
  • public boolean endsWith(String suffix)        判斷字符串是否以suffix字符串結尾
  • public String toUpperCase()        返回一個字符串為該字符串的大寫形式
  • public String toLowerCase()       返回一個字符串為該字符串的小寫形式
  • public String subString(int beginIndex)       返回該字符串從beginIndex開頭到結尾的子字符串
  • public String substring(int beginIndex, int endIndex)        返回該字符串從beginIndex開始到endIndex結尾的子字符串
  • public static String value of(...)    如public static String value of(double d)            可以將基本類型數據轉化成為字符串
  • public String[] split(String regex)        可以將一個字符串按照指令的分隔符分隔,返回分隔后的字符串數組
  • public char[] toCharArray()      將此字符串轉換為一個新的字符數組。返回:一個新分配的字符數組,它的長度是此字符串的長度,而且內容被初始化為包含此字符串表示的字符序列。
  二、StringBuffer類的用法 
  • 構造函數
  1. StringBuffer() 
  2. StringBuffer(int size) 
  3. StringBuffer(String str) 
  4. StringBuffer(CharSequence chars) 
  • length()capacity()  

      一個StringBuffer當前長度可通過length()方法得到,而整個可分配空間通過capacity()方法得到。 

  •  ensureCapacity() 設置緩沖區的大小 

      void ensureCapacity(int capacity)  

  •  setLength() 設置緩沖區的長度

      void setLength(int len)  

  • charAt()setCharAt()  

        char charAt(int where)  

       void setCharAt(int where,char ch) 

  •  getChars()  

         void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)  

  •  append() 可把任何類型數據的字符串表示連接到調用的StringBuffer對象的末尾。 

 例:int a=42;  

        StringBuffer sb=new StringBuffer(40); 

       String s=sb.append("a=").append(a).append("!").toString();  

  •  insert() 插入字符串 
  • StringBuffer insert(int index,String str)
  • StringBuffer insert(int index,char ch) 
  •  StringBuffer insert(int index,Object obj) 

       index指定將字符串插入到StringBuffer對象中的位置的下標。 

  •  reverse() 顛倒StringBuffer對象中的字符

        StringBuffer reverse()  

  •  delete()deleteCharAt() 刪除字符 

       StringBuffer delete(int startIndex,int endIndex)  

       StringBuffer deleteCharAt(int loc) 

  • replace() 替換

         StringBuffer replace(int startIndex,int endIndex,String str) 

  • substring() 截取子串
  1.         String substring(int startIndex) 
  2.          String substring(int startIndex,int endIndex)

 

public class StringBuffer_test2 { public static void main(String[] args) {  //創建可變字符串 
        StringBuffer s;        String  s1="djkkjsasahhcak"; s=new StringBuffer(s1); s1=s.toString(); //StringBuffer轉換為String //StringBuffer的常用方法 //append方法該方法的作用是追加內容到當前StringBuffer對象的末尾,類似於字符串的連接。 //調用該方法以后,StringBuffer對象的內容也發生改變,例如:s.append("flase"); s.append(true); 
        System.out.println(s); StringBuffer ss=new StringBuffer();        String s2="test";        String s3="123"; ss.append("select*form userInfo where username=").append(s2).append(" and password=").append(s3); System.out.println(ss); //deleteCharAt方法:該方法的作用是刪除指定位置的字符,然后將剩余的內容形成新的字符串。 
        StringBuffer ss1=new StringBuffer("test"); ss1.deleteCharAt(1); ss1.delete(1, 3); System.out.println(ss1); //insert方法 :該方法的作用是在StringBuffer對象中插入內容,然后形成新的字符串。例如: 
System.out.println(ss1.insert(0, false));
// System.out.println(ss1.replace(1, 3, "ks"));//插入加反轉 //setCharAt方法:該方法的作用是修改對象中索引值為index位置的字符為新的字符 ss1.setCharAt(ss1.indexOf("a"), 'k'); ss1.replace(0, 1, "12"); System.out.println(ss1); ss1.replace(0, 3, "12"); System.out.println(ss1); //trimToSize方法該方法的作用是將StringBuffer對象的中存儲空間縮小到和字符串長度一樣的長度,減少空間的浪費。 String s22=ss1.toString(); ss1.trimToSize(); System.out.println(ss1); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM