標籤:訪問 記憶體 取子串 str copy toc 指定 java 字典順序
API:(Application Programming Interface,編程介面)是一些預先定義的函數,目的是提供應用程式與開發人員基於某軟體或硬體的以訪問一組常式的能力,而又無需訪問源碼,或理解內部工作機制的細節。
String字串:
java中用String類進行描述。對字串進行了對象的封裝。這樣的好處是可以對字串這種常見資料進行方便的操作。對象封裝後,可以定義N多屬性和行為。
如何定義字串對象呢?String s = "abc";只要是雙引號引起的資料都是字串對象。
特點:字串一旦被初始化,就不可以被改變,存放在方法區中的常量池中。
------------------------------------------------------
String s1 = "abc"; // s1指向的記憶體中只有一個對象abc。
String s2 = new String("abc"); // s2指向的內容中有兩個對象abc、new 。
System.out.println(s1==s2);//false ==比較的是地址值
System.out.println(s1.equals(s2));//true ,字串中equals比較的是字串內容是否相同。
而在Object中,equals比較的是兩個對象的地址值,是否為相同。
查API。
-------------------------------------------------------
字串的方法:
1:將位元組數組或者字元數組轉成字串。
String s1 = new String();//建立了一個空內容的字串。
String s2 = null;//s2沒有任何對象指向,是一個null常量值。
String s3 = "";//s3指向一個具體的字串對象,只不過這個字串中沒有內容。
//一般在定義字串時,不用new。
String s4 = new String("abc");
String s5 = "abc"; 一般用此寫法
new String(char[]);//將字元數組轉成字串。
new String(char[],offset,count);//將字元數組中的一部分轉成字串。
2:一般方法:
2.1 擷取:
2.1.1:擷取字串的長度。length();
2.1.2:指定位置的字元。char charAt(int index);
2.1.3:擷取指定字元的位置。如果不存在返回-1,所以可以通過傳回值-1來判斷某一個字元不存在的情況。
int indexOf(int ch);//返回第一次找到的字元角標
int indexOf(int ch,int fromIndex); //返回從指定位置開始第一次找到的角標
int indexOf(String str); //返回第一次找到的字串角標
int indexOf(String str,int fromIndex);
int lastIndexOf(int ch);
int lastIndexOf(int ch,int fromIndex);
int lastIndexOf(String str);
int lastIndexOf(String str,int fromIndex);
2.1.4:擷取子串。
String substring(int start);//從start位開始,到length()-1為止.
String substring(int start,int end);//從start開始到end為止。//包含start位,不包含end位。
substring(0,str.length());//擷取整串
2.2
2.2.1:字串中包含指定的字串嗎?
boolean contains(String substring);
2.2.2:字串是否以指定字串開頭啊?
boolean startsWith(string);
2.2.3:字串是否以指定字串結尾啊?
boolean endsWith(string);
2.2.4:判斷字串是否相同
boolean equals(string);//覆蓋了Object中的方法,判斷字串內容是否相同。
2.2.5:判斷字串內容是否相同,忽略大小寫。
boolean equalsIgnoreCase(string) ;
2.3
2.3.1:通過建構函式可以將字元數組或者位元組數組轉成字串。
2.3.2:可以通過字串中的靜態方法,將字元數組轉成字串。
static String copyValueOf(char[] );
static String copyValueOf(char[],int offset,int count);
static String valueOf(char[]);
static String valueOf(char[],int offset,int count);
2.3.3:將基礎資料型別 (Elementary Data Type)或者對象轉成字串。
static String valueOf(char);
static String valueOf(boolean);
static String valueOf(double);
static String valueOf(float);
static String valueOf(int);
static String valueOf(long);
static String valueOf(Object);
2.3.4:將字串轉成大小寫。
String toLowerCase();
String toUpperCase();
2.3.5:將字串轉成數組。
char[] toCharArray();//轉成字元數組。
byte[] getBytes();//可以加入編碼錶。轉成位元組數組。
2.3.6:將字串轉成字串數組。切割方法。
String[] split(分割的規則-字串);
2.3.7:將字串進行內容替換。注意:修改後變成新字串,並不是將原字串直接修改。
String replace(oldChar,newChar);
String replace(oldstring,newstring);
2.3.8: String concat(string); //對字串進行追加。
String trim();//去除字串兩端的空格
int compareTo();//如果參數字串等於此字串,則傳回值 0;如果此字串按字典順序小於字串參數,則返回一個小於 0 的值;如果此字串按字典順序大於字串參數,則返回一個大於 0 的值。
java基礎知識(14)---API