整理string類常見方法的使用說明

2021-09-01 11:15:10 字數 1005 閱讀 5067

整理string類的length()、charat()、getchars()、replace()、touppercase()、tolowercase()、trim()、tochararray()使用說明

length():

可以用來

計算字串的長度

。例如:

string s=new string(「

asdf

」);system.out.println(s.length());

//求得字串長度。

charat():獲取指定位置的字元

。例如:

char charat(int index)

。index為字串引數,字串的序數從0到length-1

,表示從index+1個字元開始

:string s=new string(「

asdf

」);system.out.println(「s.charat(

2):」+s.charat(

2));結果為

d。 getchars():獲取從指定位置起的子串複製到字串陣列中;

void getchars(int scrbegin,int scrend,chardst,int dstbegin);四個引數的意思為:1.被拷貝字元在字串中的起始位置。2.被拷貝的最後乙個字元在字串中的下標再加1。3.目標字元陣列。4.拷貝的字元放在字元陣列中的起始下標。

replace():子串替換;

用法:replace(oldstring,newstring);用新字串代替舊字串。

touppercase()、tolowercase()大小寫轉換

,比如str.touppercase();將str中所有小寫轉換成大寫。

trim()去除頭尾空格,

比如str=」abc」,str.trim();將abc頭尾的空格去掉。

tochararray()將字串物件轉換為字元陣列,

str.tochararray();將str轉換成char陣列。

String類的常見方法整理

判斷 boolean equals object obj 比較內容是否相等 boolean equalsignorecase string str 比較內容是否相等,忽略大小寫 boolean contains string str 判斷字串是否包含字串str boolean startswith ...

string函式的常見方法

0.tolower用法 將字串轉換成小寫 string s hello string s1 s.tolower 記住字串的不可變性 console.writeline s1 1.trim的用法 用來去除字串兩邊的空白 2.split函式 用來分隔陣列的函式 常見幾種型別 string split p...

Arrays工具類常見方法

排序 sort 查詢 binarysearch 比較 equals 填充 fill 轉列表 aslist 轉字串 tostring 複製 copyof 排序 sort int a sort int a 方法按照數字順序排列指定的陣列。arrays.sort a system.out.println ...