字串的比較 查詢 替換

2021-10-20 05:35:07 字數 2039 閱讀 2865

1、public boolean equals(object anobject) //區分大小寫的比較

2、public boolean equalsignorecase(string anotherstring) //不區分大小寫的比較

3、public nt compareto(string anotherstring) //比較兩個字串大小

string str1 = "abcde";

string str2 = "abcde";

system.out.println(str1.equals(str2));

system.out.println(str1.equalsignorecase(str2));

int ret = str1.compareto(str2);

system.out.println(ret);

//結果為

false

true

32 //a的值是97,a的值是65

1、public boolean contains(charsequence s) //判斷乙個字串是否存在

2、public int indexof(string str) //從頭查詢指定字串的位置

3、public int indexof(string str,int fromindex) //從指定位置開始查詢

4、public int lastindexof(string str) //從後往前查詢字串的位置

5、public int lastindexof(string str,int fromindex) //從指定位置由後往前找字串的位置

6、public boolean startswith(string prefix) //判斷是否以指定字串開頭

7、public boolean startswith(string prefix ,int toffset) //判斷指定位置是否以指定字串開頭

8、public boolean endswith(string suffix) //判斷是否以指定字串結尾

string str = "hello world hello china";

system.out.println(str.contains("world"));//判斷字串是否含有「world」

system.out.println(str.indexof("hello"));//查詢字串「hello」的下標

system.out.println(str.indexof("hello",6));

system.out.println(str.lastindexof("hello"));

system.out.println(str.lastindexof("world",15));

system.out.println(str.startswith("hello"));

system.out.println(str.startswith("hello",12));

system.out.println(str.endswith("china"));

//結果為

true012

126true

true

true

1、public string replaceall(string regex,string replacement) //替換所有指定內容

2、public string replacefirst(string regex,string replacement) //替換首個指定內容

string str = "hello world hello china";

system.out.println(str.replaceall("hello","hello"));

system.out.println(str.replacefirst("hello","h"));

//結果為

hello world hello china

h world hello china

字串 字串的查詢和替換

hello str hello world 1.判斷是否以指定字串開始 print hello str.startswith hello 2.判斷是否以指定字串結束 print hello str.endswith world 3.查詢指定字串 index同樣可以查詢指定的字串在大字串中的索引 pr...

php 字串 包含 比較 替換

if strpos str,ok false 來判斷字串 str內是否含有 ok 在內,但最近發現用這個方式判斷乙個帶 ok 的字串卻沒有查詢 成功,鬱悶的是,這個字串就是以 ok 開頭,怎麼可能查詢不到呢?找到strpos函式的定義介紹,srtpos返回查詢字串第乙個出 現的位子,這個位置是從0開...

QT中字串的比較 查詢 替換等操作

基本操作 qstring s1 welcome qstring s2 s2 s1 to you qstring s3 hello s3 s3 world qdebug s2 endl s3 endl qstring s4 hello qdebug s4 endl qstring s5 s5.spri...