String類
實例化Stirng對象的兩種方式:
A、直接賦值
public class StringDemo01
{
public static void mian(String args[]){
String name="張三"; //直接爲String賦值
System.out.println("姓名:"+name);
}
}
B、通過關鍵字new賦值
public class StringDemo01
{
public static void mian(String args[]){
String name=new String("張三"); //使用new關鍵字進行賦值
System.out.println("姓名:"+name);
}
}
關於兩者之間的不同,看如下代碼分析:
public class StringDemo02
{
public static void main(String args[]){
String name1="張三";
String name2="張三";
String name3=new String("張三");
String name4=new String("張三");
System.out.println("equals方法的比較:"+name1.equals(name3)); //-->true
System.out.println("==的比較:"+name1==name2); //-->true
System.out.println("==方法的比較:"+name1==name3); //-->false
System.out.println("==方法的比較:"+name3==name4); //-->false
}
}
equals方法與==的區別:
在java中,str1.equals(str2)方法比較的是兩個字符串是否相等,而==,表示的是隻有兩個引用都指向了同一個對象時纔會返回true;
String直接賦值和使用new關鍵字兩種實例化方式的區別:
1、採用直接賦值的方式其實JVM自動創建了一個匿名對象,當爲String直接賦值的時候會自動在堆內存中進行查找,如果在池中找到了值爲”張三”的字符串,就不會再重新開闢空間,直接使用當前堆內存的中的字符串的值。所以name1==name2返回爲true。
2、而當使用new關鍵字的時候,都會在堆中創建,此時開闢的就是兩個空間,其中有一個是垃圾的空間,需要等待垃圾的回收。
綜上所述,在java的開法中通常是使用爲String直接賦值的方式,創建相對較少的對象,從而節省內存資源。
·字符串的內容不能改變
String一旦聲明則不能改變其值,原因是String本身是一個類,而在java中類屬於引用數據類型,這一點較爲重要,如果想要改變字符串的值,則最好使用StringBuffer類。
代碼解釋:
使用String 值不會被改變
public class StringDemo02
{
public static void main(String args[]){
String name1="張三";
change(name1);
System.out.println(name1); //結果 張三
}
public static void change(String str){
str="你好";
}
}
使用StringBuffer 值會被改變:
public class StringDemo02
{
public static void main(String args[]){
StringBuffer name1=new StringBuffer("張三");
change(name1);
System.out.println(name1); //結果 張三你好
}
public static void change(StringBuffer str){
str.append("你好");
}
}
String類的常用方法
String類的常用方法:
·字符數組與字符串
一個字符串可以變爲一個字符數組,同樣,也可以把一個字符數組,變爲一個字符串。
在String類中提供了一下的操作方法:
·將字符串變爲字符數組:public char[] toCharArray()
public class StringApiDemo01
{
public static void main(String args[]){
String a="hello";
char c[]=a.toCharArray(); //將字符串變爲字符數組
for(int i=0;i<c.length;i++){
System.out.print(c[i]);
}
}
}
·將字符數組變爲字符串:
1、 採用構造方法的形式:
·public String(char[] value)
·String(char[] value, int offset, int count) 根據字符數組的開始點以及長度轉爲字符串
2、 採用方法的形式:
public static void valueOf(char[] data)
public class StringApiDemo02
{
public static void main(String args[]){
char c[]={'h','e','l','l','o'};
String str1=new String(c); //將字符數組變爲字符串
String str2=new String(c,0,3); //從數組的第0個索引取3個長度轉爲字符串
String str3=new String().valueOf(c); //使用方法返回字符串
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
·從字符串中取出指定位置的字符
方法:public char charAt(int index)
public class StringApiDemo02
{
public static void main(String args[]){
String str="hello"; //定義字符串
System.out.println(str.charAt(3)); //取出第四個字符
}
}
·字符串與byte數組的轉換
·將byte數組變爲字符串:public byte[] getBytes()
|-:將一個字節數組變爲字符串:public String (byte[] bytes)
|-:將部分字節數組變爲字符串:public String (byte[] bytes,int offset,int length)
public class StringApiDemo03
{
public static void main(String args[]){
String str="hello"; //定義字符串
byte b[]=str.getBytes();
String str1=new String(b); //將byte數組變爲字符串
String str2=new String(b,0,3); //將部分byte數組變爲字符串
System.out.println(str1);
System.out.println(str2);
}
}
·取得一個字符串的長度
方法:public int lenth()
public class StringApiDemo04
{
public static void main(String args[]){
String str="hello"; //定義字符串
System.out.println("\""+str+"\"的長度爲:"+str.length());
}
}
注意:數組的長度爲屬性,即:數組名.length
字符串的長度爲方法,即:字符串.length()
·查找指定的字符串是否存在
·從頭開始查找:public int indexOf(String str)
·從指定位置開始查找:public int indexOf(String str,int fromIndex)
·擴展:指定字符在此字符串中最後一次出現處的索引。public int lastIndexOf(int ch)
兩種查找的方式最終都會返回int類型的數據,此數據表示的是一個字符串的具體位子,如果沒有查找到此字符串,則會返回-1
public class StringApiDemo05
{
public static void main(String args[]){
String str="hellcollc"; //定義字符串
System.out.println(str.indexOf("c")); //從頭開始查找c
System.out.println(str.indexOf("c",5)); //從指定位置開始查找c
System.out.println(str.indexOf("x")); //查找x 若沒有 則會返回-1
System.out.println(str.lastIndexOf("c")); //查找c最後一次出現的索引位置
}
}
·去掉空格
假如對用於的輸入進行去空格的操作,此種方法只是會去掉字符串左右兩邊的空格,中間的空格是不能被去掉的。
public class StringApiDemo06
{
public static void main(String args[]){
String str=" hello "; //定義字符串
System.out.println(str); //輸出帶空格
System.out.println(str.trim()); //輸出沒有空格
}
}
·字符截取
·從指定位置開始截取到結束位置:public String substring(int beginIndex)
·截取指定範圍的字符串:public String substring(int beginIndex,int endIndex)
public class StringApiDemo07
{
public static void main(String args[]){
String str="hello world"; //定義字符串
System.out.println(str.substring(5)); //從第6個位置開始截取直到最後
System.out.println(str.substring(3,6)); //從第4個位置開始截取,到低7個位置
}
}
注意:假如此時指定索引超出字符串的大小,則會拋出java.lang.StringIndexOutOfBoundsException 該異常繼承自IndexOutOfBoundsException。
·拆分字符串
如果需要按指定的字符串去拆分另一個字符串:public String[] split(String regex)
代碼示例:按指定字符進行拆分,並賦值給一個新的字符串
public class StringApiDemo08
{
public static void main(String args[]){
String str="hello world"; //定義字符串
String str1=""; //定義一個字符串用來接收
String s[]=str.split(" "); //按空格進行拆分
for(String x:s){ //循環輸出
str1+=x; //每循環一次爲str1添加一個字符
System.out.print(x);
}
System.out.println(str1); //得到str1的值
}
}
·大小寫轉換
此功能在開發中會經常用到,將一個字符串全部字母變爲小寫或者將一個小寫的字符串中的全部字母全部變爲大寫。
·大寫變小寫:public void toUpperCase()
·小寫變大寫:public void toLowerCase()
public class StringApiDemo09
{
public static void main(String args[]){
System.out.println("將\"hello\"轉大寫"+"hello".toUpperCase());
System.out.println("將\"HELLO\"轉小寫:"+"HELLO".toLowerCase());
}
}
·判斷是否以指定的字符串開頭或結尾
·判斷是否以指定的字符串開頭:public boolean startsWith(String prefix);
·判斷是否以指定的字符串結尾:public boolean endsWith(String suffix);
public class StringApiDemo10
{
public static void main(String args[]){
String str="**hello";
String str1="hello**";
System.out.println(str.startsWith("*")); //判斷是否以*開頭 true
System.out.println(str1.endsWith("*")); //判斷是否以*結尾 true
}
}
·不區分大小寫的比較
在String類中equals()方法是用來比較字符串是否相等,但是此種方法只是針對大小寫一樣的字符串來說的,如果不想區分大小寫來進行比較,可以採用以下方法:
public boolean equalsIgnoreCase(String anotherString)
public class StringApiDemo11
{
public static void main(String args[]){
String str="HELLO";
String str1="hello";
System.out.println("\""+str+"\"和"+"\""+str1+"\"區分大小寫的比較:"+str.equals(str1));
System.out.println("\""+str+"\"和"+"\""+str1+"\"不區分大小寫的比較:"+str.equalsIgnoreCase(str1));
}
}
·字符串的替換功能
將字符串按照指定的格式進行全部替換:
public class StringApiDemo12
{
public static void main(String args[]){
String str1="hello";
String str2=str1.replaceAll("l","x"); //將字符串中的所有l替換爲x
System.out.println(str2);
}
}
this關鍵字
this關鍵字的作用:
1、 表示本類或者是父類的方法
2、 表示類中的屬性
3、 可以使用this調用本類中的構造方法
4、 this表示當前對象
表示類中的屬性
class Person
{
private String name; //姓名
private int age; //年齡
public Person(String name,int age){ //構造方法,爲Person類中的屬性初始化值
this.name=name;
this.age=age;
}
public void getInfo(){
System.out.println("姓名是: "+name+"年齡是: "+age);
}
}
public class thisDemo01
{
public static void main(String args[]){
Person person=new Person("張三",20);
person.getInfo();
}
}
使用this調用構造方法
如果在一個類中有多個構造方法的話,也可以使用this關鍵字互相調用。
class Person
{
private String name; //姓名
private int age; //年齡
public Person(){
System.out.println("新的對象被實例化");
}
public Person(String name){
this(); //調用無參的構造方法
this.name=name;
}
public Person(String name,int age){ //構造方法,爲Person類中的屬性初始化值
this(name); //調用有一個參數的構造方法
this.age=age;
}
public void getInfo(){
System.out.println("姓名是: "+name+"年齡是: "+age);
}
}
public class thisDemo02
{
public static void main(String args[]){
Person person=new Person("張三",20);
person.getInfo();
}
}
注意:在使用this關鍵字調用其它構造方法時,有以下的限制:
·this調用構造方法的語句只能放在構造方法的首行。
·在使用this調用本類中其它構造方法時,至少有一個構造方法是不能用this調用的,原因是構造方法出現了遞歸調用,此時程序會出現錯誤。
this關鍵字表示當前對象
當前對象:當前正在調用方法的對象
class Person
{
public String getInfo(){
System.out.println("Person類:"+this); //直接打印this
return null; //爲保證語句正確,直接返回null
}
}
public class thisDemo03
{
public static void main(String args[]){
Person per1=new Person(); //通過構造方法實例化對象
Person per2=new Person(); //通過構造方法實例化對象
System.out.println("MAIN方法:"+per1); //直接打印對象
per1.getInfo(); //當前調用getInfo方法的是per1對象
System.out.println("MAIN方法:"+per2); //直接打印對象
per2.getInfo(); //當前調用getInfo方法的是per2對象
}
}
對象比較
可以使用this和引用傳遞進行兩個對象是否相等的判斷。
class Person
{
private String name;
private int age;
public Person(String name,int age){
this.setName(name);
this.setAge(age);
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public boolean compare(Person per){ //直接傳遞本類對象,方便的調用類中的私有屬性
//地址值相等,則兩個對象肯定是相等的
if(this==per){
return true;
}
//地址值假如對象不相等 則判斷各自對象的屬性值是否相等
else if(this.name.equals(per.name)&&this.age==per.age){
return true;
}
else{
return false;
}
}
}
public class thisDemo04
{
public static void main(String args[]){
Person per1=new Person("張三",20); //通過構造方法實例化對象
Person per2=new Person("張三",30); //通過構造方法實例化對象
if(per1.compare(per2)){
System.out.println("相等!");
}
else{
System.out.println("不相等!");
}
}
}