String類的常用方法演示

2021-08-30 22:31:37 字數 3174 閱讀 6671

public class demo2 else

//out:i love you!

//使用public int codepointat(int index)

for(int i = 0 ; i < str.length() ; i ++)

/*字母"i"的ascii碼是:73

字母" "的ascii碼是:32

字母"l"的ascii碼是:108

字母"o"的ascii碼是:111

字母"v"的ascii碼是:118

字母"e"的ascii碼是:101

字母" "的ascii碼是:32

字母"y"的ascii碼是:121

字母"o"的ascii碼是:111

字母"u"的ascii碼是:117

字母"!"的ascii碼是:33

*/system.out.println("字串" + "\"" + str + "\"" +

"的第乙個字母的assci碼是:" + str.codepointbefore(1) + " ");

//out:字串"i love you!"的第乙個字母的assci碼是:73

system.out.println("字串" + "\"" + str + "\"" + "的長度是:" +

str.codepointcount(0,str.length()));

//out:字串"i love you!"的長度是:11

system.out.println(new string("i love you,").concat("vividle!"));

//out:i love you,vividle!

//使用public int compareto(string anotherstring)

if(0 == new string("love").compareto("love"))else

//out:the two string isn't the same.

if(0 == new string("love").comparetoignorecase("love"))else

//out:if we ignore case ,the two string is the same.

//使用public boolean matches(string regex)

system.out.println(new string("match").matches("match"));//out:true

system.out.println(new string("matche").regionmatches(0,"match",0,6));//out:false

system.out.println(new string("match").regionmatches(true,0,"match",0,5));//out:true

//使用public int offsetbycodepoints(int index, int codepointoffset)

string abcdef = new string(new char);

int index = abcdef.offsetbycodepoints(abcdef.length(),-3);

system.out.println(index);//out:3

int cp = abcdef.codepointat(index);

system.out.println(cp);//out:100

//使用 public void getchars(int srcbegin, int srcend, char dst, int dstbegin)

char chget = new char[11];

str.getchars(0, 11, chget, 0);

system.out.println(chget);//out:i love you!

//使用ublic boolean contentequals(stringbuffer sb)

system.out.println(str.contentequals(new stringbuffer("i don't love you!")));//out:false

//使用public boolean startswith(string prefix, int toffset)

system.out.println(str.startswith("i",0));//out:true

system.out.println(str.hashcode());//out:-1503998791

// public int indexof(string str, int fromindex)

system.out.println(str.indexof("l",0));//out:2

// public charsequence subsequence(int beginindex, int endindex)

system.out.println(str.subsequence(0,11).tostring());//out:i love you!

//public string replace(char oldchar, char newchar)

system.out.println(str.replace(' ','*'));//out:i*love*you!

//public string replacefirst(string regex, string replacement)

system.out.println(str.replacefirst(" ","*"));//out:i*love you!

// public string replaceall(string regex, string replacement)

system.out.println(str.replaceall(" ","*"));//out:i*love*you!

//public string split(string regex, int limit)

string strarray = new string{};

strarray = str.split(" ",0);

for(string x :strarray)

/** i

love

you! */ }

}

String 類常用方法

字串 就是由多個字元組成的一串陣列 一旦被複製,就不能被改變 public class stringdemo string s2 new string bys system.out.println s2 s2 system.out.println s2.length s2.length 5 syst...

String類常用方法

方法名稱 型別 方法描述 public string char value 構造 將字元陣列變為string類物件 public string char value,int offset int count 構造 將部分字元陣列變為string類物件 public char charat int i...

String類常用方法

返回字串長度 public int length 返回字串中指定位置的字元 public char charat int index 提取字串 方法說明 public string substring int beginindex 從beginindex位置起,從當前字串中取出剩餘的字元作為乙個新的...