1. 程式人生 > >java中String的常用方法

java中String的常用方法

大寫 緩沖 let 常用方法 類型 http 全部 new 出現

轉自http://www.cnblogs.com/crazyac/articles/2012791.html

java中String的常用方法
1、length() 字符串的長度
  例:char chars[]={‘a‘,‘b‘.‘c‘};
    String s=new String(chars);
    int len=s.length();

2、charAt() 截取一個字符
  例:char ch;
    ch="abc".charAt(1); 返回‘b‘

3、 getChars() 截取多個字符
  void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
  sourceStart指定了子串開始字符的下標,sourceEnd指定了子串結束後的下一個字符的下標。因此, 子串包含從sourceStart到sourceEnd-1的字符。接收字符的數組由target指定,target中開始復制子串的下標值是targetStart。
  例:String s="this is a demo of the getChars method.";
    char buf[]=new char[20];
    s.getChars(10,14,buf,0);

4、getBytes()
  替代getChars()的一種方法是將字符存儲在字節數組中,該方法即getBytes()。

5、toCharArray()

6、equals()和equalsIgnoreCase() 比較兩個字符串

7、regionMatches() 用於比較一個字符串中特定區域與另一特定區域,它有一個重載的形式允許在比較中忽略大小寫。
  boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
  boolean regionMatches(boolean ignoreCase,int startIndex,String str2,int str2StartIndex,int numChars)

8、startsWith()和endsWith()  startsWith()方法決定是否以特定字符串開始,endWith()方法決定是否以特定字符串結束

9、equals()和==
  equals()方法比較字符串對象中的字符,==運算符比較兩個對象是否引用同一實例。
  例:String s1="Hello";
    String s2=new String(s1);
    s1.eauals(s2); //true
    s1==s2;//false

10、compareTo()和compareToIgnoreCase() 比較字符串

11、indexOf()和lastIndexOf()
  indexOf() 查找字符或者子串第一次出現的地方。
  lastIndexOf() 查找字符或者子串是後一次出現的地方。

12、substring()  它有兩種形式,第一種是:String substring(int startIndex)
         第二種是:String substring(int startIndex,int endIndex)

13、concat() 連接兩個字符串

14 、replace() 替換
  它有兩種形式,第一種形式用一個字符在調用字符串中所有出現某個字符的地方進行替換,形式如下:
  String replace(char original,char replacement)
  例如:String s="Hello".replace(‘l‘,‘w‘);
  第二種形式是用一個字符序列替換另一個字符序列,形式如下:
  String replace(CharSequence original,CharSequence replacement)

15、trim() 去掉起始和結尾的空格

16、valueOf() 轉換為字符串

17、toLowerCase() 轉換為小寫

18、toUpperCase() 轉換為大寫

19、StringBuffer構造函數
  StringBuffer定義了三個構造函數:
  StringBuffer()
  StringBuffer(int size)
  StringBuffer(String str)
  StringBuffer(CharSequence chars)
  
  (1)、length()和capacity()    一個StringBuffer當前長度可通過length()方法得到,而整個可分配空間通過capacity()方法得到。
  
  (2)、ensureCapacity() 設置緩沖區的大小
    void ensureCapacity(int capacity)

  (3)、setLength() 設置緩沖區的長度
    void setLength(int len)

  (4)、charAt()和setCharAt()
    char charAt(int where)
    void setCharAt(int where,char ch)

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

  (6)、append() 可把任何類型數據的字符串表示連接到調用的StringBuffer對象的末尾。
    例:int a=42;
      StringBuffer sb=new StringBuffer(40);
      String s=sb.append("a=").append(a).append("!").toString();

  (7)、insert() 插入字符串
    StringBuffer insert(int index,String str)
    StringBuffer insert(int index,char ch)
    StringBuffer insert(int index,Object obj)
    index指定將字符串插入到StringBuffer對象中的位置的下標。

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

  (9)、delete()和deleteCharAt() 刪除字符
    StringBuffer delete(int startIndex,int endIndex)
    StringBuffer deleteCharAt(int loc)

  (10)、replace() 替換
    StringBuffer replace(int startIndex,int endIndex,String str)

  (11)、substring() 截取子串
    String substring(int startIndex)
    String substring(int startIndex,int endIndex)

例子:
//String所給出的方法均可以直接調用
public class Test{
public static void main(String[] args){
String s = "Welcome to Java World!";
String s1 = " sun java ";
System.out.println(s.startsWith("Welcome"));//字符串以Welcome開頭
System.out.println(s.endsWith("World"));//字符串以World結尾
String sL = s.toLowerCase();//全部轉換成小寫
String sU = s.toUpperCase();//全部轉換成大寫
System.out.println(sL);
System.out.println(sU);
String b = s.substring(11);//從第十一位開始
System.out.println(b);
String c = s.substring(8,11);//從第八位開始在第十一位結束
System.out.println(c);
String d = s1.trim();//去掉首尾的空格
System.out.println(d);
String s2 = "我是程序員,我在學java";
String e = s2.replace("我","你");
System.out.println(e);
int f = 5;
String s3 = String.valueOf(f);
System.out.println(s3);
String s4 = "我是,這的,大王";
String[] g = s4.split(",");
System.out.println(g[0]);

java中String的常用方法