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); } }

 

原文地址:https://www.cnblogs.com/xiaoxiaohui2015/p/5832464.html