String類常用操作總結

2021-10-13 10:30:50 字數 2144 閱讀 8791

;//indexof(ch)返回字元所在位置索引下標

system.out.

println

(file.

indexof

("."))

;//indexof(str)返回字元所在位置索引下標

system.out.

println

(file.

indexof

('j',0

)); system.out.

println

(file.

indexof

("j",2

));//indexof(str,fromindex),從指定索引開始,找字串第一次出現的下標,

system.out.

println

(file.

indexof

("j",3

));//找不到返回-1

//返回字串最後出現的索引下標

system.out.

println

( file.

lastindexof

("."))

;/**substring:返回乙個新字串,它是此字串的乙個子字串。該子字串始於指定索引處的字元,一直到此字串末尾。

引數:beginindex - 開始處的索引(包括), endindex - 結束處的索引(不包括)

如果 beginindex或endindex 為負或大於此 string物件的長度,則丟擲indexoutofbound***ception

**/system.out.

println

( file.

substring

(file.

lastindexof

(".")+

1));

//返回指定索引處的值

system.out.

println

( file.

charat(0

));//拼接/合併字串

system.out.

println

(file.

concat

("aaaaa"))

;//是否包含該字串返回boolean值

;//是否是length為0的空字串即 ""

system.out.

println

(file.

isempty()

);//是否以img結尾

system.out.

println

(file.

endswith

("img"))

;//去空格

system.out.

println

(file.

trim()

);//replace 替換 jpg-被替換為img, .被替換為空

));//比較倆字串的值,字元比較asii碼,數字比較大小,>1, <-1, =0

;//轉為byte位元組

trycatch

(unsupportedencodingexception e)

string類的常用操作總結

include string str 空字串 string s str 生成空字串str的複製品輸入輸出不要使用c語言的標準輸入輸出,如scanf,printf,gets cin str 空格結尾輸入 getline cin,str 換行符結尾輸入 輸入字串時,也可以使用其他分隔符作為字串結束的標誌...

總結String類常用方法

public class test 1.獲取字串長度 system.out.println str.length 輸出62.判斷字串是否一致 區分大小寫 system.out.println str.equals abcd 輸出false3.判斷字串是否一致 不區分大小寫 system.out.pr...

總結String類常用方法

public class test string str abuiugba system.out.println str.indexof a system.out.println str.lastindexof a 從右向左數a的位置 a還是正常順序的第乙個a system.out.println ...