黑馬程式設計師 Java String 類

2021-06-19 13:43:23 字數 3356 閱讀 4848

---------------------- asp.net+android+ios開發、.net培訓、期待與您交流! ----------------------

1:string類

(1)字串:多個字元組成的一串資料。

(2)構造方法:

a:string s = new string();

b:string s = new string(byte bys);

c:string s = new string(byte bys,int index,int length);

d:string s = new string(char chs);

e:string s = new string(char chs,int index,int length);

f:string s = new string(string str);

g:string s = "hello";

(3)字串的特點及面試題

a:字串一旦被賦值,就不能改變。

注意:字串的值不能改變,沒有說引用變數不能改變。

b:面試題:

a:string s = new string("hello")和string s = "hello"的區別。

b:請寫出結果:

string s1 = new string("hello");

string s2 = new string("hello");

system.out.println(s1==s2); //false

system.out.println(s1.equals(s2));//true

string s3 = new string("hello");

string s4 = "hello";

system.out.println(s3==s4);//false

system.out.println(s3.equals(s4));//true

string s5 = "hello";

string s6 = "hello";

system.out.println(s5==s6);//true

system.out.println(s5.equals(s6));//true

(4)成員方法

a:判斷功能

boolean equals(object obj)

將此字串與指定的物件比較。當且僅當該引數不為 null,

並且是與此物件表示相同字串行的 string 物件時,結果才為 true。

boolean equalsignorecase(string str)

與equals功能相同並且不考慮大小寫

boolean contains(string str)

當且僅當此字串包含指定的 str 值序列時,返回 true。

boolean startswith(string str)

測試此字串是否以指定的字首開始。

boolean endswith(string str)

測試此字串是否以指定的字尾結束。

boolean isempty()

當且僅當 length() 為 0 時返回 true。 

b:獲取功能

int length()

獲取字串的長度

char charat(int index)

返回指定索引處的 char 值。

int indexof(int ch)

返回指定字元在此字串中第一次出現處的索引。

int indexof(string str)

返回指定子字串在此字串中第一次出現處的索引。

int indexof(int ch,int fromindex)

返回在此字串中第一次出現指定字元處的索引,從指定的索引開始搜尋。

int indexof(string str,int fromindex)

返回指定子字串在此字串中第一次出現處的索引,從指定的索引開始。

string substring(int start)

返回乙個新的字串,它是此字串的乙個子字串。該子字串從指定索引處的字元開始,

直到此字串末尾。

string substring(int start,int end)

返回乙個新字串,它是此字串的乙個子字串。該子字串從指定的 beginindex 處開始,

直到索引 endindex - 1 處的字元。

c:轉換功能

byte getbytes()

使用平台的預設字符集將此 string 編碼為 byte 序列,並將結果儲存到乙個新的 byte 陣列中。

char tochararray()

將此字串轉換為乙個新的字元陣列。

static string copyvalueof(char chs)

返回指定陣列中表示該字串行的 string。

static string valueof(char chs)

返回 char 陣列引數的字串表示形式。

static string valueof(int i)

返回 int 引數的字串表示形式。

string tolowercase()

使用預設語言環境的規則將此 string 中的所有字元都轉換為小寫。

string touppercase()

使用預設語言環境的規則將此 string 中的所有字元都轉換為大寫。

string concat(string str)

將指定字串連線到此字串的結尾。

d:其他功能

a:替換功能

string replace(char oldchar,char newchar)

返回乙個新的字串,它是通過用 newchar 替換此字串中出現的所有 oldchar 得到的。

string replace(string oldstring,string newstring)

b:切割功能

string split(string regex)

根據匹配給定的正規表示式來拆分此字串。

c:去除兩端空格功能

string trim()

返回字串的副本,忽略前導空白和尾部空白。

d:字典順序比較功能

int compareto(string str)

按字典順序比較兩個字串

int comparetoignorecase(string str) 

按字典順序比較兩個字串,不考慮大小寫。

---------------------- asp.net+android+ios開發、.net培訓、期待與您交流! ----------------------

黑馬程式設計師

asp.net android ios開發 net培訓 期待與您交流!map hashtable 底層是雜湊表資料結構,不可以存入null鍵和null值。該執行緒是同步的,jdk1.0出現,效率低。hashmap 底層是雜湊表資料結構,允許使用null鍵和null值。該集合執行緒是不同步的,將has...

黑馬程式設計師

1.了解反射 反射 就是通過class檔案物件,去使用該檔案中的成員變數,構造方法,成員方法。person p new person p.使用 要想這樣使用,首先你必須得到class檔案物件,其實也就是得到class類的物件。2.反射中的類 class類 成員變數 field類 構造方法 const...

黑馬程式設計師

黑馬程式設計師 j a基礎學習筆記2 一 筆記內容概述 關鍵字 識別符號 注釋 注釋的應用 常量 進製的由來 進製轉換 負數的進製 變數的介紹 變數的基本演示 型別提公升 強制轉換 字元型別運算過程 型別運算細節 算術運算子 賦值運算子。二 常用內容介紹 1.注釋內容不占用記憶體,即使新增再多的注釋...