1.'=='、'.equals'和'contains'的區別:java
值類型是存儲在內存中的棧,而引用類型的變量在棧中僅僅是存儲引用類型變量的地址,而類型變量自己則存儲在堆中。數組
==操做比較的是兩個變量的值是否相等,對於引用型變量表示的是兩個變量在堆中存儲的地址是否相同(是否引用的同一個東西,故也許兩個字符串表時內容相同,但存儲在不一樣位置,仍會報False),即棧中的內容是否相同。app
equals操做表示的兩個變量是不是對同一個對象的引用,即堆中的內容是否相同。this
contains操做表示當前字符串是否含有參數指定的字符串編碼
==比較的是2個對象的地址,而equals比較的是2個對象的內容,contains當字符串a與字符串b相等時,仍返回True。spa
顯然,當equals爲true時,==不必定爲true;對象
例子:blog
public class Example{
public static void main(String args[ ]){
String s1,s2;
s1=new String("we are students");
s2=new String("we are students");
System.out.print(s1.equals(s2)+" ");
System.out.println(s1==s2);
String s3,s4;
s3="how are you";
s4="how are you";
System.out.print(s3.equals(s4)+" ");
System.out.println(s3==s4);
System.out.print(s1.contains(s3)+" ");
System.out.println(s2.contains("we are students"));
}
}
結果:內存
True Falseci
True True
False True
2.String類的經常使用方法
1)先後綴:public boolean startsWith(String s)
public boolean endsWith(String s) 判斷當前字符串的先後綴是否與指定字符串s相等。
2)字典序比較:
public int compareTo(String s) 按字典序與當前指定的字符串比較大小。
3)子串的檢索:
public int indexOf (String s) 從當前字符串的頭開始檢索字符串s,並返回首次出現s的位置,若沒有檢索到字符串s,返回值爲-1。
indexOf(String s ,int startpoint) 從startpoint位置開始檢索字符串s,並返回首次出現s的位置,若沒有檢索到字符串s,返回值爲-1。
lastIndexOf (String s) 從當前字符串的頭開始檢索字符串s,並返回最後一次出現s的位置,若沒有檢索到字符串s,返回值爲-1.
4)字符串的截取:
public String substring(int startpoint) 從startpoint位置開始截取到字符串的最後一個位置,並返回所得的字符串。
substring(int start ,int end) 從start處開始截取,到end處中止,但end處字符不包含在其中,即只有(end-start)個字符。
5)字符串去空格:
public String trim():去掉字符串的先後空格。
3.字符串與基本數據類型的轉換(java.lang包)
1)字符串轉數字:
public int static parseInt(String s):將數字類型字符串s轉換爲int型數據。
public byte static parseByte(String s);
public short static parseShort(String s);
public long static parseLong(String s);
public double static parseDouble(String s);
2)整數的進製表示(返回字符串):
public static String toBinaryString(long i) 整數i的二進制表示;
public static String toOctalString(long i) 十進制;
public static String toHexString(long i) 十六進制;
public static String toString(long i,int p) 整數i的p進製表示;
3)數字的字符串表示:
public String valueOf(byte n)
public String valueOf(int n)
public String valueOf(long n)
public String valueOf(float n)
public String valueOf(double n)
4.對象的字符串表示
全部的類都默認是java.lang包中Object類的子類或間接子類。Object類有一個public方法public String toString(),一個對象經過調用該方法能夠得到該對象的字符串表示 。
一個類能夠經過重寫該方法,以便得到該對象想要的字符串表示。(java.util包中的Date類就經過重寫該方法使得Date對象調用toString()獲得的字符串是由日期信息組成的字符序列。
若一個類沒有重寫該方法,則該類所建立的對象調用該方法獲得的字符串格式爲:
類名@對象的引用。
例:
import java.util.Date;
public class Example{
public static void main(String args[ ]){
Date date=new Date();
Student stu=new Student("張三",89);
TV tv=new TV("電視機",8776);
System.out.println(date.toString());
System.out.println(stu.toString());
System.out.println(tv.toString());
}
}
class Student{
String name;
double score;
Student(String name,double score){
this.name=name;
this.score=score;
}
public String toString(){
return "姓名:"+name+",分數:"+score;
}
}
class TV{
String name;
double price;
TV(String name,double price){
this.name=name;
this.price=price;
}
}
輸出結果爲:
5.字符串與字符數組、字節數組
1):字符串與字符數組
類的構造方法String(char[])和String(char[],int offset,int length)分別用數組a中的所有字符和部分字符建立字符串對象。
String類也提供了將字符串存放到數組中的方法:
public void getChars(int start,int end,char c[],int offset)
字符串調用getChars()方法將當前字符串中的一部分字符複製到參數c指定的數組中,將字符串從位置start到end-1位置上的字符複製的數組c中,並從數組c的offset處開始存放這些字符。
String類還提供了一個方法 public char[] to CharArray()
字符串對象調用該方法能夠初始化一個字符數組,改庶族的長度與字符串的長度相等,並將字符串對象的所有字符複製到該數組中。
2):字符串與字節數組
String類的構造方法String(byte[])用指定的字節數組構造一個字符串對象。構造方法String(byte[],int offset,int length)用指定的字節數組的一部分,即從數組起始位置offset開始取length個字節構造一個字符串對象。方法public byte[] getBytes()使用平臺默認的字符編碼,將當前字符串轉化爲一個字節數組。
例:
public class Example{
public static void main(String args[ ]){
byte d[]="YOUIHE你我他".getBytes();
System.out.println("數組d的長度是(一個漢字佔兩個字節):"+d.length);
String s=new String(d,6,2);
System.out.println(s);
}
}
6.StringBuffer類
String類建立的字符串對象不能修改、刪除或替換字符串中的某個字符,即String對象一旦建立,實體是不能再發生變化的。而StringBuffer類對象的實體的內存能夠自動的改變大小,經過調用append()方法能夠追加字符串序列。
1)構造方法:
StringBuffer():分配給該對象的實體的初始容量可容納16個字符,且容量會自動增長。
StringBuffer(int size):指定初始長度,能夠指定分配給該對象的實體的初始容量爲size指定的字符個數,且容量會自動增長(至關於這裏用size代替前者的「16」)。
StringBuffer(String s):用一個字符串指定初始長度 。
2)經常使用方法:
a.追加字符串:append()。
b.取單個字符:char charAt(int n)獲得參數n指定的位置上的單個字符。
c.替換其中單個字符:void setCharAt(Int n,char ch)將當前StringBuffer對象實體中的字符串位置n處的字符用參數ch指定的字符替換。
d.插入字符串:StringBuffer insert(int index,String str)將一個字符串插入另外一個字符串中,並返回當前對象的引用。
e.刪除子串:StringBuffer delete(int startIndex,int endIndex)從當前StringBuffer對象實體中的字符串中刪除一個子字符串,並返回當前對象的引用。
f.字符串替換:StringBuffer replace(int startIndex,int endIndex,String str)將當前對象實體中的字符串的一個子字符串用參數str制定的字符串替換。
g.字符串反轉:public StringBuffer reverse()將對象實體中的字符翻轉,並返回當前對象的引用。
3)length與capacity的區別:
length返回當前引用實體的實際長度,capacity則返回當前StringBuffer的容量(即默認不斷+16,16爲預留空間,雖然沒有存儲東西,但在capacity中會算入容量,而在length中不算)。
7.StringTokenizer類
1)構造方法:
a.StringTokenizer(String s):爲字符串s構造一個分析器,使用默認空格分隔標記。
b.StringTokenizer(String s,String delim):爲字符串s構造一個分析器,參數delim中的字符的任意排列組合都是分隔標記。
例:用戶輸入浮點數,程序分別輸出該書的整數部分和小數部分。
import java.util.*; public class Example{ public static void main(String args[ ]){ String []mess={"整數部分","小數部分"}; Scanner reader=new Scanner(System.in); double x=reader.nextDouble(); String s=String.valueOf(x); StringTokenizer fenxi=new StringTokenizer(s,"."); for(int i=0;fenxi.hasMoreTokens();i++){ String str=fenxi.nextToken();//將分隔符前的String賦給整數部分 System.out.println(mess[i]+":"+str); } } }