1.字符串基礎(chǔ)
概念:字符串本質(zhì)是打包字符數(shù)組的對象,是java.lang.String類的實例
2.字符串的構(gòu)造方法
public String()
public String(byte[] bytes)
public String(byte[] bytes, int offset, int length)
public String(char[] value)
public String(char[] value, int offset, int count)
public String(String original)
3.字符串特性
(1)字符串常量與字符串池
以””包括的字符串,只要內(nèi)容相同,無論在程序代碼中出現(xiàn)了幾次,JVM都只會建立一個String實例,并在字符串池中維護(hù)。
【注】以””寫下的字符串稱為字符串常量

(2)不可變動字符串
字符串一旦被創(chuàng)建,它的內(nèi)容就不能被改變。下面代碼用+連接了兩個字符串,實踐上又new了一個對象

4.常用方法
4.1將字符串轉(zhuǎn)換為基本類型(以下方法時基本類型中的方法)
Byte.parseByte(String s)
Short.parseShort(String s)
Integer.parseInt(String s)
Long.parseLong(String s)
Float.parseFloat(String s)
Float.valueOf(String s)
Double.parseDouble(String s)
Double.valueOf(String s)
4.2.字符串的功能
(1)判斷功能
public boolean equals(Object anObject)
public boolean contentEquals(StringBuffer sb)
public boolean contains(CharSequence s)
public boolean startsWith(String PRefix, int toffset)
public boolean endsWith(String suffix)
public boolean isEmpty()
(2)獲取功能
public int length()
public char charAt(int index)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
public int indexOf(int ch, int fromIndex)【
ch- 一個字符(Unicode 代碼點)】public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
(3)轉(zhuǎn)換功能
public byte[] getBytes()
public char[] toCharArray()
public static String valueOf(char c)【一個長度為
1的字符串】public static String valueOf(int i)
public String toLowerCase()
public String toUpperCase()
public String concat(String str)【將指定字符串連接到此字符串的結(jié)尾】
(4)其他功能
A.替換功能
public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)【字符集即字符串】
B.去空格功能
public String trim()
C.按字典比較功能
public int compareTo(String anotherString)【比較基于字符串中各個字符的 Unicode 值】
public int compareToIgnoreCase(String str)
新聞熱點
疑難解答