String字串用法整理

2021-09-26 05:35:03 字數 3475 閱讀 4021

字串的構造方法

public string()//string s = new string();

public string(string original)//s = new string(「hello」);

public string(char value)**** 將字元陣列轉換成字串s = new string(new char );

public string(char value, int index, int count)//s = new string(new char , 1, 2);

public string(byte bytes)**** 將位元組陣列轉換成字串//s = new string(new byte );

public string(byte bytes, int offset, int length)//s = new string(new byte , 1, 2);

string(byte bytes, string charsetname)處理字串編碼問題

​ //編碼的操作

​ string s = 「今晚攻打高老莊」;

​ byte bys = s.getbytes(「utf-8」);

​ // 解碼的操作

​ string result = new string(bys, 「utf-8」);

​ system.out.println(result);

注:如果編譯碼方式不一樣會導致亂碼

---------------------------------分割線----------------------------------

以下**的用法是尋找索引值

char charat(int index)

int indexof(int ch)

int indexof(string str)

int indexof(int ch,int fromindex)

int indexof(string str,int fromindex)

int lastindexof(int ch) int lastindexof(int ch,int fromindex)

int lastindexof(string str,int fromindex)

---------------------------------分割線----------------------------------

string substring(int start)//擷取字串,可以設定起始位置

string substring(int start,int end)//擷取字串,可以設定開頭結尾

int length()//計算字串的長度

---------------------------------分割線----------------------------------

boolean isempty():判斷字串是否為空。

boolean equals(object obj):將此字串的內容與指定的物件比較,區分大小寫。

boolean equalsignorecase(string str):將此 string 與另乙個 string 比較,忽略大小寫。

boolean contains(string str):判斷字串中是否包含方法傳入的字串。

boolean startswith(string str):判斷字串是否以某個指定的字串開頭。

boolean endswith(string str):判斷字串是否以某個指定的字串結尾。

---------------------------------分割線----------------------------------

byte getbytes():將字串轉化為位元組陣列。

char tochararray(): 將字串轉化為字元陣列。

static string valueof(char chs): 返回 char 陣列引數的字串表示形式。

static string valueof(int i):返回 int 引數的字串表示形式。

string tolowercase():將此 string 中的所有字元都轉換為小寫。

string touppercase():將此 string 中的所有字元都轉換為大寫。

string concat(string str): 將指定字串連線到此字串的結尾。

---------------------------------分割線----------------------------------

string replace(char old,char new):替換功能。

string replace(string old,string new):替換功能。

string trim():去除字串兩空格。

int compareto(string str):按字典順序比較兩個字串。

int comparetoignorecase(string str):按字典順序比較兩個字串,忽略大小寫。

public string split(string regex):分隔字串成字元陣列。

---------------------------------分割線----------------------------------

static string format(locale l, string format, object… args)

使用指定的區域設定,格式字串和引數返回格式化的字串。

static string format(string format, object… args)

使用指定的格式字串和引數返回格式化的字串。

String字串用法集合

一 string字串不可更改 當連線兩個字串的時候,改變的是字串的形,而位址是不可更改的 比如 str1 hello str2 world 則連線str1 和str2 之後的執行結果為 helloworld 但是一共開闢了 3個記憶體空間,str1 str2 以及連線之後。例如 public cla...

string字串函式用法

申明 str1 str2為string型別,c1,c2為字元陣列 1.字串長度 str1.length strlen c1 2.字串後加文字 對應c函式strcat c1,c2 返回指標c1 3.字串和字元陣列相互轉換 字元陣列轉化為字串 str1 c1 或者 string str1 c1,c1 n...

String字串常見用法

字串大小寫轉換string str1 yao,jiffffng,的尾巴,ggg 字串大小寫轉換 system.err.println 小寫 str1.tolowercase system.out.println 大寫 str1.touppercase string string str1.subst...