Java String 類小結

來源:CSDN   作者:ravili   
 
 
1.String (char a[]) 用一個字符數組a 創建一個字符串對象,如 
  char a[]={‘b’,’o’,’y’}; 
  String s=new String(a); 
  上述過程相當於 String s= "boy"; 

2. String(char a[],int startIndex,int count) 提取字符數組a 中的一部分字符創建一個字符串對象 ,參數startIndex 和count 分別指定在a 中提取字符的起始位置和從該位置開始截取的字符個數,例如 
char a[]={‘s’,’t’,’b’,’u’,’s’,’n’}; 
String s=new String(a,2,3); 
上述過程相當於 String s= "bus"; 


3 equals方法 equalsIgnoreCase方法忽略大小寫 
字符串對象調用String類中的public boolean equals(String s)方法比較當前字符串對象的實體是否與參數指定的字符串s的實體相同.如 
String tom=new String( "we are students"); 
String boy=new String( "We are students"); 
String jerry= new String("we are students"); 

tom.equals(boy)的值是false, 
tom.equals(jerry)的值是 true. 

4 startsWith,endsWith方法 
String tom= "220302620629021",jerry= "21079670924022"; 
tom.startsWith("220")的值是true 
jerry.startsWith("220")的值是false. 


5 regionMatches方法 
字符串調用 
public boolean regionMatches(int firstStart,String other,int ortherStart,int length) 
方法,從當前字符串參數firstStart指定的位置開始處,取長度爲length的一個子串,並將這個子串和參數other 指定的一個子串進行比較,其中,other 指定的子串是從參數othertStart 指定的位置開始,從other中取長度爲length的一個子串.如果兩個子串相同該方法就返回true,否則返回false. 
使用該方法的重載方法 
public boolean regionMatches(boolean b,int firstStart,String other,int ortherStart,intlength) 
可以通過參數b決定是否忽略大小寫,當b取true時,忽略大小寫. 


6 compareTo,compareToIgnoreCase方法 
字符串對象可以使用String類中的 
public int compareTo(String s)方法,按辭典序與參數s指定的字符串比較大小. 
如果當前字符串與s相同,該方法返回值0 
如果當前字符串對象大於s,該方法返回正值 
如果小於s,該方法返回負值. 


7 public int indexOf (String s) 字符串調用該方法從當前字符串的頭開始檢索字符串s,並返回首次出現s的位置.如果沒有檢索到字符串s,該方法返回的值是-1. 

public int indexOf(String s ,int startpoint) 字符串調用該方法從當前字符串的startpoint 位置處開始檢索字符串s,並返回首次出現s 的位置.如果沒有檢索到字符串s,該方法返回的值是-1. 

public int lastIndexOf (String s) 字符串調用該方法從當前字符串的頭開始檢索字符串s,並返回最後出現s的位置.如果沒有檢索到字符串s,該方法返回的值是-1. 

public int lastIndexOf(String s ,int startpoint) 字符串調用該方法從當前字符串的startpoint 位置處開始檢索字符串s,並返回最後出現s 的位置.如果沒有檢索到字符串s,該方法返回的值是-1. 


8 public String substring(int startpoint) 字符串對象調用該方法獲得一個當前字符串的子串,該子串是從當前字符串的startpoint處截取到最後所得到的字符串. 

public String substring(int start ,int end) 字符串對象調用該方法獲得一個當前字符串的子串,該子串是從當前字符串的start 處截取到end 處所得到的字符串,但不包括end處所對應的字符. 


9 public String replace(char oldChar,char newChar) 字符串對象s調用該方法可以獲得一個串對象,這個串對象是用參數newChar 指定的字符替換s 中由oldChar 指定的所有字符而得到的字符串. 

public String replaceAll(String old ,String new) 字符串對象s調用該方法可以獲得一個串對象,這個串對象是通過用參數new指定的字符串替換s 中由old 指定的所有字符串而得到的字符串. 

Public String trim() 一個字符串s 通過調用方法trim()得到一個字符串對象,該字符串對象是s去掉前後空格後的字符串. 


10 轉化爲整型 
java.lang包中的Integer類調用其類方法 
public static int parseInt(String s) 
可以將 “數字” 格式的字符串,如"12387",轉化爲int型數據.例如 
int x; 
String s="6542"; 
x=Integer.parseInt("6542"); 
類似地,使用java.lang包中的Byte,Short,Long類調相應的類方法 
public static byte parseByte(String s) 
public static short parseShort(String s) 
public static long parseLong(String s) 
可以將“數字”格式的字符串,轉化爲相應的基本數據類型 


11 轉化爲float型或double型java.lang包中的Float類調用其類方法 
  public static int parseFloat (String s)可以將 “數字”格式的字符串,如"12387.8976",轉 化爲float 型數據.例如 
  float n=Float.parseFloat("12387.8976") 
或 
  String s= new String(“12387.8976”); 
  float n=Float.parseFloat(s) 


12 public StringvalueOf( byte n) 
  public StringvalueOf (int n) 
  public StringvalueOf (long n) 
  public StringvalueOf (float n) 
  public StringvalueOf (double n) 

將形如123,1232.98等數值轉化爲字符串,如 
String str=String.valueOf(12313.9876); 
float x=123.987f; 
String temp=String.valueOf(x); 


13 將字符串中的字符拷貝到字符數組 
public void getChars(int start,int end,char c[],int offset ) 字符串調用getChars 方法將當前字符串中的一部分字符拷貝到參數c 指定的數組中,將字符串中從位置start 到end-1位置上的字符拷貝的數組c中,並從數組c 的offset處開始存放這些字符.需要注意的是,必須保證數組c能容納下要被拷貝的字符. 

 原文:[url]http://www.wnetw.net/article/javase/autofile/20070710165911.html[/url]


本文來源:[url]http://blog.csdn.net/ravili/archive/2007/09/29/1806952.aspx[/url]
本文地址:[url]http://www.newbooks.com.cn/info/157895.html[/url]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章