java常見類庫之String類詳解

2021-06-29 00:03:49 字數 4140 閱讀 1904

常用構造方法

string()

初始化乙個新建立的 string 物件,使其表示乙個空字串行。

string(byte bytes)

通過使用平台的預設字符集解碼指定的 byte 陣列,構造乙個新的 string。

string(byte bytes, charset charset)

通過使用指定的 charset 解碼指定的 byte 陣列,構造乙個新的 string。

string(char value)

分配乙個新的 string,使其表示字元陣列引數中當前包含的字串行。

例項化:

1.string str=」sss」

2.string str=new string();

把其他格式變數轉換string如int,bool等一般用valueof()函式。

下面給出string常用的方法。

方法:

char charat(int index)

返回指定索引處的 char 值。

int compareto(string anotherstring)

按字典順序比較兩個字串。

string concat(string str)

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

boolean contains(charsequence s)

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

boolean contentequals(charsequence cs)

將此字串與指定的 charsequence 比較。

boolean contentequals(stringbuffer sb)

將此字串與指定的 stringbuffer 比較。

boolean endswith(string suffix)

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

boolean equals(object anobject)

將此字串與指定的物件比較。

boolean equalsignorecase(string anotherstring)

將此 string 與另乙個 string 比較,不考慮大小寫。

byte getbytes()

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

byte getbytes(charset charset)

使用給定的 charset 將此 string 編碼到 byte 序列,並將結果儲存到新的 byte 陣列。

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)

返回指定子字串在此字串中最後一次出現處的索引,從指定的索引開始反向搜尋。

int length()

返回此字串的長度。

boolean matches(string regex)

告知此字串是否匹配給定的正規表示式。

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)

測試此字串從指定索引開始的子字串是否以指定字首開始。

char tochararray()

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

string tolowercase()

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

string tolowercase(locale locale)

使用給定 locale 的規則將此 string 中的所有字元都轉換為小寫。

string tostring()

返回此物件本身(它已經是乙個字串!)。

string touppercase()

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

string touppercase(locale locale)

使用給定 locale 的規則將此 string 中的所有字元都轉換為大寫。

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語言基礎之String類常見功能詳解

上次說到string類的一些特性和在記憶體中的情況。這篇文章主要從方法功能入手,講一講string類的應用,還有一些stringbuffer和stringbuilder的區別及應用。廢話不多說,進入正題。關於對資料的基本操作,無非就是 1.增添2.刪除3.修改4.檢視 1.檢視 1.1檢視長度 in...

Java 基礎之String類

1.string類初始化方法 public class main string str3 new string chars string str4 new string chars,1,4 system.out.println str1 str1 system.out.println str2 st...

Java基礎之String類

1.字串不變 字串的值在建立後不能被更改。2.因為string物件是不可變的,所以它們可以被共享。即記憶體中只建立了乙個物件,可以被多個使用 3.string字串相當於乙個陣列,string底層是靠字元陣列實現的。1.無參構造 string str new string 2.通過字元陣列構造 str...