String類的一些特點以及使用方法

2021-09-19 13:51:20 字數 3005 閱讀 3972

*string類

*string 特點

// 利用多型來建立字串 char charat(int index)

charsequence sequence = "abc";

string s = "helloworldhello";

system.out.println("charat " + s.charat(0));

system.out.println("charat " + s.charat(1));

// 遍歷字串沒乙個字元 int indexof(int ch)

for (int i = 0; i < s.length(); i++)
// int indeof(int ch)

system.out.println("indexof(int ch): " + s.indexof("l"));
// int indexof(string str)返回某個字串對應的索引

system.out.println("int indexof(string str): " + s.indexof("world"));
// 從fromindex處開始從左往右找,第一次出現字元ch所對應的索引

system.out.println("int indexof(int ch, int fromindex): " + s.indexof('l', 4));
// 從字串左往右開始找,第一次出現字串所對應的位置

system.out.println("int indexof(string string, int fromindex): " + s.indexof("hello", 6));
// 從右往左開始找第一次字元對應的索引

system.out.println("int lastindexof(int ch): " + s.lastindexof('l'));
// 從右往左找,第一次出現的

system.out.println("int lastindexof(int ch, int fromindex): " + s.lastindexof('l', 10));
// 從右往左找,第乙個字串對應的索引

system.out.println("string string, int fromindex: " + s.lastindexof("hello", 10));
// 擷取

s.substring(10);

system.out.println(s);

string s1 = s.substring(5, 10); //左閉右開原則

system.out.println(s1);

charsequence subsequence = s.subsequence(5, 10);

system.out.println(sequence);

string s = "helloworld";

// 將字串轉化為位元組陣列

byte bys = s.getbytes();

system.out.println(arrays.tostring(bys));

for (byte b : bys)

system.out.println("------------");

// 將字串轉化為字元陣列

char ch = s.tochararray();

for (char c : ch)

system.out.println("-------------");

// 返回char陣列引數的字串表示

string ss1 = string.valueof(false);

system.out.println(ss1);

string ss3 = string.valueof(2.5);

string ss2 = string.valueof(10);

system.out.println(ss2 + ss3);

string ss4 = string.valueof(new object());

system.out.println(ss4);

system.out.println("--------------");

string touppercase = "select * from t_student".touppercase();

system.out.println(touppercase);

string lowercase = touppercase.tolowercase();

system.out.println(lowercase);

system.out.println("---------------");

system.out.println("hello".concat("world"));

*string replace(char old.char new): 替換功能

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

*string trim(): 去除字串的空格

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

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

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

*/public class seatwork4 ;

string join = string.join("+", chs);

system.out.println(join);

}

String類的特點

1 了解string類 1.string類是被final修飾的,是不能被繼承的 2.string類底層使用陣列結構 jdk9以前使用的是char jdk9以後使用的是byte 3.string的物件一旦建立就不能修改 底層維護了乙個字串常量池,實現共享 注意 string類的物件每次修改都會產生乙個...

String類的一些問題

string a hello string b hello string c he llo string d he new string llo a b 1 a c 2 a d 3首先公布答案,式子1返回true,式子2返回true,式子3返回false 式子1很好理解,由於存在字面量池,在用字面量...

string類的一些成員函式

1 const char data data 函式返回指向自己的第乙個字元的指標.由於data 函式返回的是const char 所以不能直接通過指標修改返回的字串的值,如果要修改,必須把型別轉化為char 2 const char c str c str 函式返回乙個指向正規c字串的指標,內容與本...