1. 程式人生 > >String字符串方法具體解釋

String字符串方法具體解釋

字符串替換 ring float args 語言環境 comm format obj phi

Java開發中,基本都會用戶String,有些時候忘記了它還有某一個方法,或者曾經沒有使用到。而這些方法可能會節約非常多時間。自己為了學習這些方法,決定對部分測試一下。
定義:String=”atm”;

System.out.println();//這裏為打印結果

以下開始測試各個方法

char charAt(int index) 
          返回指定索引處的 char 值。

System.out.println(a.charAt(0));//a

int codePointAt(int index) 
          返回指定索引處的字符(Unicode 代碼點)。 
     System.out
.println(a.codePointAt(1));//116
int codePointBefore(int index) 
          返回指定索引之前的字符(Unicode 代碼點)。
          System.out.println(a.codePointBefore(1));//97
int codePointCount(int beginIndex, int endIndex) 
          返回此 String 的指定文本範圍中的 Unicode 代碼點數。 
          System.out.println(a.codePointCount(0
,1));//1
int compareTo(String anotherString) 
          按字典順序比較兩個字符串。 
          System.out.println(a.compareTo("ATM1"));//32
      關於compareTo能夠看另外一篇文章
      [字符串比較](http://blog.csdn.net/qq_17326933/article/details/47452467)      
int compareToIgnoreCase(String str) 
          不考慮大寫和小寫,按字典順序比較兩個字符串。
          System.out
.println(a.compareToIgnoreCase("atm"));//0
String concat(String str) 
          將指定字符串聯到此字符串的結尾。 
          System.out.println(a.concat("BANK"));//atmBANK
boolean contains(CharSequence s) 
          當且僅當此字符串包括 char 值的指定序列時,才返回 true。
          System.out.println(a.contains("a")); //true  
boolean contentEquals(CharSequence cs) 
       當且僅當此 String 表示與指定序列同樣的 char 值時,才返回 true

boolean contentEquals(StringBuffer sb) 
          當且僅當此 String 表示與指定的 StringBuffer 同樣的字符序列時,才返回 true。

System.out.println(a.contentEquals("atm"));//true System.out.println(a.contentEquals("atm1"));//false

static String copyValueOf(char[] data) 
         返回指定數組中表示該字符序列的字符串。
         char[] test = new char[]{‘a‘,‘b‘,‘c‘};
         String a = String.valueOf(test);
         String b = String.copyValueOf(test);
         System.out.println(a);//abc
         System.out.println(b);//abc
static String copyValueOf(char[] data, int offset, int count) 
          返回指定數組中表示該字符序列的字符串。 
          char[] test = new char[]{‘a‘,‘b‘,‘c‘};
          String b = String.copyValueOf(test,1,2);
          System.out.println(b);//bc
boolean endsWith(String suffix) 
          測試此字符串是否以指定的後綴結束。 
          String a="atm";
          System.out.println(a.endsWith("atm"));//true
          System.out.println(a.endsWith("tm"));//true
          System.out.println(a.endsWith("m"));//true
boolean equals(Object anObject) 
          比較此字符串與指定的對象。 
          能夠看另外一篇文章
          [equales與==](http://blog.csdn.net/qq_17326933/article/details/47188879)
boolean equalsIgnoreCase(String anotherString) 
          將此 String 與還有一個 String 進行比較,不考慮大寫和小寫。

static String format(Locale l, String format, Object... args) 
          使用指定的語言環境、格式字符串和參數返回一個格式化字符串。

網上有一篇文章,總結的非常具體: [JAVA字符串格式化-String.format()的使用](http://blog.csdn.net/lonely_fireworks/article/details/7962171)

static String format(String format, Object... args) 
          使用指定的格式字符串和參數返回一個格式化字符串。

byte[] getBytes() 
          使用平臺默認的字符集將此 String 解碼為字節序列,並將結果存儲到一個新的字節數組中。
           byte[] bb=a.getBytes(); 
void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) 
          已過時。 該方法無法將字符正確轉換為字節。

從 JDK 1.1 起,完畢該轉換的首選方法是通過 getBytes() 構造方法。該方法使用平臺的默認字符集。

byte[] getBytes(String charsetName) 
          使用指定的字符集將此 String 解碼為字節序列,並將結果存儲到一個新的字節數組中。 
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 
          將字符從此字符串拷貝到目標字符數組。

int hashCode() 
          返回此字符串的哈希代碼。 
int indexOf(int ch) 
          返回指定字符在此字符串中第一次出現處的索引。 
int indexOf(int ch, int fromIndex) 
          從指定的索引開始搜索,返回在此字符串中第一次出現指定字符處的索引。 
int indexOf(String str) 
          返回第一次出現的指定子字符串在此字符串中的索引。 
          System.out.println(a.indexOf("t"));//1
int indexOf(String str, int fromIndex) 
          從指定的索引處開始,返回第一次出現的指定子字符串在此字符串中的索引。

String a="atmat"; System.out.println(a.indexOf("t",2));//true

String intern() 
          返回字符串對象的規範化表示形式。

int lastIndexOf(int ch) 
          返回最後一次出現的指定字符在此字符串中的索引。

int lastIndexOf(int ch, int fromIndex) 
          從指定的索引處開始進行後向搜索。返回最後一次出現的指定字符在此字符串中的索引。 
int lastIndexOf(String str) 
          返回在此字符串中最右邊出現的指定子字符串的索引。 
           //要獲取最後的11.doc 
          String a="c/sys/atm/map/11.doc";
          System.out.println(a.substring(a.lastIndexOf("/")+1));
int lastIndexOf(String str, int fromIndex) 
          從指定的索引處開始向後搜索,返回在此字符串中最後一次出現的指定子字符串的索引。 
int length() 
          返回此字符串的長度。
boolean matches(String regex) 
          通知此字符串是否匹配給定的正則表達式。 
int offsetByCodePoints(int index, int codePointOffset) 
          返回此 String 中從給定的 index 處偏移 codePointOffset 個代碼點的索引。
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 
          測試兩個字符串區域是否相等。

boolean regionMatches(int toffset, String other, int ooffset, int len) 
          測試兩個字符串區域是否相等。 
String replace(char oldChar, char newChar) 
          返回一個新的字符串,它是通過用 newChar 替換此字符串中出現的全部 oldChar 而生成的。 
String replace(CharSequence target, CharSequence replacement) 
          使用指定的字面值替換序列替換此字符串匹配字面值目標序列的每一個子字符串。

String replaceAll(String regex, String replacement) 
          使用給定的 replacement 字符串替換此字符串匹配給定的正則表達式的每一個子字符串。 
String replaceFirst(String regex, String replacement) 
          使用給定的 replacement 字符串替換此字符串匹配給定的正則表達式的第一個子字符串。 
String[] split(String regex) 
          依據給定的正則表達式的匹配來拆分此字符串。 
String[] split(String regex, int limit) 
          依據匹配給定的正則表達式來拆分此字符串。 
boolean startsWith(String prefix) 
          測試此字符串是否以指定的前綴開始。 
boolean startsWith(String prefix, int toffset) 
          測試此字符串是否以指定前綴開始。該前綴以指定索引開始。

CharSequence subSequence(int beginIndex, int endIndex) 
          返回一個新的字符序列。它是此序列的一個子序列。 
String substring(int beginIndex) 
          返回一個新的字符串,它是此字符串的一個子字符串。 
String substring(int beginIndex, int endIndex) 
          返回一個新字符串,它是此字符串的一個子字符串。 
char[] toCharArray() 
          將此字符串轉換為一個新的字符數組。

String toLowerCase() 
          使用默認語言環境的規則將此 String 中的全部字符都轉換為小寫。 
String toLowerCase(Locale locale) 
          使用給定 Locale 的規則將此 String 中的全部字符都轉換為小寫。 
String toString() 
          返回此對象本身(它已經是一個字符串!

)。

String toUpperCase() 
          使用默認語言環境的規則將此 String 中的全部字符都轉換為大寫。
String toUpperCase(Locale locale) 
          使用給定的 Locale 規則將此 String 中的全部字符都轉換為大寫。 
String trim() 
          返回字符串的副本。忽略前導空白和尾部空白。 
static String valueOf(boolean b) 
          返回 boolean 參數的字符串表示形式。

static String valueOf(char c) 
          返回 char 參數的字符串表示形式。 
static String valueOf(char[] data) 
          返回 char 數組參數的字符串表示形式。 
static String valueOf(char[] data, int offset, int count) 
          返回 char 數組參數的特定子數組的字符串表示形式。 
static String valueOf(double d) 
          返回 double 參數的字符串表示形式。 
static String valueOf(float f) 
          返回 float 參數的字符串表示形式。 
static String valueOf(int i) 
          返回 int 參數的字符串表示形式。 
static String valueOf(long l) 
          返回 long 參數的字符串表示形式。 
static String valueOf(Object obj) 
          返回 Object 參數的字符串表示形式。 

從類 java.lang.Object 繼承的方法 clone, finalize, getClass, notify, notifyAll, wait, wait, wait

String字符串方法具體解釋