字串String類方法Demo

2021-07-14 14:54:57 字數 2787 閱讀 6565

/*

string類適用於描述字串事物。

那麼它就提供了多個方法對字串進行操作。

常見的操作有哪些?

"abcd"

1。獲取。

1.1 字串中的包含的字元數,也就是字串的長度。

int length();獲取長度。區別於陣列的length屬性,帶()是方法

1.2 根據位置獲取位置上某個字元。

char charat(int index):

1.3 根據字元獲取該字元在字串中位置。

int indexof(int ch);返回的是ch在字串中第一次出現的位置。

int indexof(int ch,int fromindex):從fromindex指定位置開始,獲取ch在字串中出現的位置.

int indexof(string str):返回的是str在字串中第一次出現的位置.

int indexof(string str,int fromindex):從fromindex指定位置開始,獲取str在字串中出現的位置.

int lastindexof(int ch):

2,判斷

2.1字串中是否包含某乙個子串

boolean contains(str):

特殊:indexof(str):可以索引str第一次出現位置,如果返回-1,表示該str不在字串中存在.

所以,也可以用於對指定判斷是否包含.

if(str.indexof("aa")!=-1)

而且該方法即可以判斷,又可以獲取出現的位置.

2.2字元中是否有內容。

boolean isempty();原理就是判斷長度是否為0."",null

2.3字串是否是以制定內容開頭。

boolean startwith(str);

2.4字串是否是以制定內容結尾。

boolean endswith(str);

2.5 判斷字串內容是否相同.複寫了object類中的equals方法.

boolean equals(str);

2.6 判斷內容是否相同,並忽略大小寫.

boolean equalsignorecase();

3.轉換

3.1 將字元陣列至成字串.

建構函式:string(char)

string(char,offset起始下標,count個數):將字元陣列中的一部分轉成字串.

靜態方法:

static string copyvalueof(char);

static string copyvalueof(char data, int offset, int count)

static string valueof(char);

3.2 將字串轉成字元陣列.

char tochararray();

3.3 將位元組陣列轉成字串.

string(byte)

string(byte,offset起始下標,count個數):將字元陣列中的一部分轉成字串.

3.4 將字串轉成位元組陣列.

byte getbytes();

3.5 將基本資料型別轉成字串

static string valueof(int)

static string valueof(double)

//自動型別轉換 轉換成字串3+"";

特殊:字串和位元組陣列在轉換過程中,是可以指定編碼表的

4,替換

string replace(oldchar,newchar);

5,切割

string split(regex);

6, 子串,獲取字串中的一部分。

string substring(begin);

string substring(begin,end);

7.轉換,去除空格,比較。

7.1 將字串轉成大寫或者小寫。

string touppercase();

string tolowercase();

7.2 將字串兩端的多個空格去除.

string trim();

7.3 對兩個字串進行自然順序的比較.

int compareto(string);

*/class stringmethoddemo

public

static

void

method_sub()

public

static

void

method_split()

public

static

void

method_trans()

;string s = new string(arr);//以 陣列arr 為引數傳入new例項化的string s

sop("s="+s);

string s1 = "zxcvbnm";

char chs = s1.tochararray();

for(int x=0;x"chs="+chs);}}

public

static

void

method_is()

public

static

void

method_get()

public

static

void

main(string args)

class static

void sop(object obj)

}

String字串類實現

1 class string 2 建構函式string string const char str else string str hell0 3 析構函式string string 4 拷貝構造string string const string other string s1 hello str...

關於字串 string類

1 字串 字串是儲存在記憶體的連續位元組中的一系列字元。儲存在連續位元組中的一系列字元意味著可以將字串儲存在char陣列中,其中每個字元都位於自己的陣列元素中。什麼時候char陣列是string型別?char dog 8 not a string char cat 8 a string 只有第二個陣...

String字串工具類

1 using system 23 namespace sam.oa.common425 26 處理字串 27 28 字串29 30 public static string processstring object obj 3134 35 判斷字串是否為日期 36 37 日期字串 38 39pub...