java中截取字符串的方式


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)

 

(12)對正反斜杠的截取

 

java中split的應用:

Java中的 split  函數是用於按指定字符(串)或正則去分割某個字符串,結果以字符串數組形式返回;

例如:

 

[java]  view plain  copy
 
  1. String str="1234@abc";  
  2. String[] a = str.split("@");  
  3. System.out.println("處理結果: "+a[0]+","+a[1]);   //輸出的是: 處理結果: 1234,abc  


對於分割的字符(串),通常是常見,普通的,沒什么問題;

但是對某些特殊字符,如果字符(串)正好是正則的一部分,則需要轉義才能使用,

這些字符有 | , + , * , ^ , $ , / , | , [ , ] , ( , ) , - , . , \等, 因它們是正則表達式中的一部分, 所以如果想用該字符本身, 這些字符需要進行轉義才能表示它本身;

例如: 

 

想用 | 豎線去分割某字符,因 | 本身是正則表達式中的一部分,所以需要 \ 去轉義,因轉義使用 \, 而這個 \ 正好也是正則表達式的字符,所以還得用一個 \ , 所以需要兩個 \\。

 

[java]  view plain  copy
 
  1. String str="5678|XYZ";  
  2. String[] b = str.split("\\|");  //注意這里用兩個 \\,而不是一個\  
  3. System.out.println("處理結果: "+b[0]+","+b[1]);   //輸出的是: 處理結果: 5678,XYZ  

再來看看:

 

[java]  view plain  copy
 
  1. String str="5678|XYZ";  
  2. String[] b = str.split("|");  //注意直接使用|,該字符是正則表達式的一部分,  
  3. String x="處理結果: ";  
  4. for(int i=0;i<b.length;i++){  
  5.     x=x+b[i]+",";  
  6. }  
  7. System.out.println(x);   //輸出的是: 處理結果: 5,6,7,8,|,X,Y,Z,  


可能我們人為主觀感覺是用 | 來分割希望得到 5678 和 XYZ,因用特殊字符,實際結果是得到意外的結果;

 

 

今天下午就這個地方耗了幾個小時,頭暈才發現問題所在,555....

 

所以指定分割的字符(串)時,最好不要包含用於正則表達式本身的字符,如上面的紅色字符;

 


免責聲明!

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



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