String類常見功能

2021-09-25 06:49:38 字數 1847 閱讀 1805

1.獲取:

1.1獲取字串中字元的個數(長度)

int length();
1.2根據位置獲取字元

char charat(int index);
1.3根據字元或字串獲取在字串中第一次出現的位置

從前往後找:

int indexof(int ch);

int indexof(int ch,int fromindex); 從指定位置進行ch的查詢

int indexof(string str);

int indexof(string str,int fromindex); 從指定位置進行str的查詢

從後往前找:

int lastindexof(int ch);

int lastindexof(int ch,int fromindex); 從指定位置進行ch的查詢

int lastindexof(string str);

int lastindexof(string str,int fromindex); 從指定位置進行str的查詢

1.4獲取字串中一部分字串,也叫子串。

string substring(int beginindex)

string substring(int beginindex,int endindex) 包含begin不包含end

2.轉換:

2.1將字串變成字串陣列

string split(string regex);  涉及正規表示式
2.2將字串變成字元陣列

char tochararray();
2.3將字串變成位元組陣列

byte getbytes();
2.4將字串中的字母轉成大小寫

string touppercase();大寫

string tolowpercase();小寫

2.5將字串中的內容進行替換

string replace(char oldchar,char newchar);

string replace(charsequence target, charsequence replacement);

2.6去除字串兩端的空格

string trim();
2.7將字串進行連線

string concat(string);   相當於+
3.判斷

3.1兩個字串內容是否相同

boolean equals(object obj);

boolean equalsignorecase(string str);忽略大小寫比較字串內容

3.2字串中是否包含指定字串

boolean contains(string str);
3.3字串是否以指定字串開頭,是否以指定字串結尾

boolean startswith(string);

boolean endswith(string);

4.比較

int compareto(string);  按字典順序比較兩個字串,若引數字串等於此字串,則值為0 ; 如果這個字串的字典比字串引數小,則值小於0; 如果此字串的字典大小超過字串引數,則值大於0 。

String常見功能

1.1 獲取字串的長度 int length 獲取長度 返回型別為int 1.2 根據位置獲取位置上某個字元 char charat int index 如果下標超出字串長度,則爆字串下標越界異常 1.3 根據字元獲取該字元在字串中位置 int indexof int ch 返回的是ch在字串中第一...

String類的常見功能和使用

string類適用於描述字串食物 那麼他就提供了多個方法對字串進行操作 常見的操作有哪些?1,獲取 1.1,字串中包含的字元數,也就是字串的長度 int length 獲取長度 1.2,根據位置獲取位置上的某個字元 char charat int index 1.3,根據字元獲取該字元在字串中的位置...

java語言基礎之String類常見功能詳解

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