String類的常用方法總結

2021-07-27 10:15:06 字數 3567 閱讀 8592

字串變為字元陣列:

public

char tochararray()

字元陣列變為字串:使用string的構造方法實現

public

string(char value)

public

string(char value,int offset,int count)

public

char

charat(int index)

字串轉換為位元組陣列:

public

byte getbytes()

位元組陣列變為字串:

public

string(byte bytes)

public

string(byte bytes, int offset, int length)

public

intlength()

the index of

thefirst occurrence of

thecharacter

inthe

character sequence represented by this object, or -1

ifthe

character

does

not occur.

public int indexof(int ch, int fromindex)

the index of

thefirst occurrence of

thecharacter

inthe

character sequence represented by this object that

isgreater than

orequal

to fromindex, or -1

ifthe

character

does

not occur.

public int lastindexof(int ch)

the index of

thelast occurrence of

thecharacter

inthe

character sequence represented by this object, or -1

ifthe

character

does

not occur.

public int lastindexof(int ch,int fromindex)

the index of

thelast occurrence of

thecharacter

inthe

character sequence represented by this object that

isless than or equal

to fromindex, or -1

ifthe

character

does

not occur before

that point.

public

string

trim()

public string substring(int beginindex)

public string substring(int beginindex, int endindex)

public

string split(string regex)

public

string split(string regex,int limit)

字串中的全部大寫字母變為小寫字母:

public string tolowercase()
字串中的全部小寫字母變為大學字母:

public string touppercase()
判斷是否以制定字串開頭:

public

boolean

startswith(string prefix)

判斷是否以制定字串結尾:

public

boolean

endswith(string suffix)

區分大小寫的字串相等比較

public

boolean

equals(object anobject)

不區分大小寫的字串相等比較

public

boolean

equalsignorecase(string anotherstring)

a string derived from this string

by replacing every occurrence of oldchar with newchar.

public string replace(charsequence target,

charsequence replacement)

replaces each substring of this string

that matches the literal target sequence with

the specified literal replacement sequence. the replacement proceeds from

thebeginning

ofthe

string

tothe

end, for example, replacing "aa"

with

"b"in

thestring

"aaa" will result

in"ba" rather than "ab".

public string replaceall(string regex,

string replacement)

replaces each substring of this string

that matches the

given regular expression with

thegiven replacement.

public string replacefirst(string regex,

string replacement)

replaces the

first substring of this string

that matches the

given regular expression with

thegiven replacement.

還是檢視api文件靠譜,省時省力,而且是第一手資料。

總結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 ...

String類中的常用方法總結

package com.qfedu.b string 字串比較問題 public class demo1 注意 無論什麼時候兩個字串比較內容是否相等,我們一定要有equals方法,因為如果用的是來比較,會比較兩個字串的位址。一般只有boolean型別的資料用來進行比較 獲取字串長度方法 int le...