------- android培訓、java培訓、期待與您交流! ---------
String 類是最終類,沒有類
字符串最大特點:一旦被初始化,不會再改變
class StringDemo
{
public static void main(String[] args)
{
String s="abc"; //s是一個類類型變量,“abc”是一個對象
String s1=new String("abc");
System.out.println(s.equals(s1));<span style="white-space:pre"> </span>
System.out.println(s1==s);
}
}
執行結果是:
true
false
因爲s指向的是“abc”對象,而s1指向了一個新的字符串對像,所以s==s1是false,這個比較的是字符串內存地址值
而字符串複寫了Object的equals方法,比較兩個字符串的內容是否相同
對於上面,s代表1個對象,s1有兩個對象(new就新建一個對象,而“abc”又是一個字符串對象)
class StringDemo
{
public static void main(String[] args)
{
String s="abc"; //s是一個類類型變量,“abc”是一個對象
String s1=new String("abc");
String s2="abc";
System.out.println(s.equals(s1));
System.out.println(s1==s);
Systetm.out.println(s==s2);
}
}
執行結果是:
true
false
true
說明s2不會在開闢一片空間存儲“abc”,因爲其已經存在了,所以s2也指向了"abc",所以s==s3
字符串的常見操作
1.獲取:
獲取字符串中字符的個數,即字符串的長度 :int length();
根據位置獲取位置上的某個字符:char charAt(int index);
根據字符獲取字符在字符串中的位置:int indexOf(int ch);()裏面是字符對應的ASC碼,所以是Int類型的,返回的是第一次出現字符的位置
int indexOf(int ch,int fromIndex)獲取從指定位置fromIndex開始出現的字符位置
int indexOf(String str)獲取字符串中首次出現str的位置
int indexOf(String str,int fromIndex)獲取str在字符串從指定索引處fromIndex處開始出現的位置
class StringDemo
{
public static void main(String[] args)
{
String s="abc"; //s是一個類類型變量,“abc”是一個對象
String s1=new String("abc");
String s2="abc";
method_get();
System.out.println(s.equals(s1));
System.out.println(s1==s);
System.out.println(s==s2);
}
public static void method_get()
{
String str="abcdegha";
//長度
sop(str.length()); //此處可以將int型的基本數據類型作爲Object對象傳入,是因爲JDK1.5版本之後出現的新特性:基本數據類型的提升,裝箱
//根據索引獲取字符
sop(str.charAt(3));
//根據字符獲取索引,如果沒有,返回-1
sop(str.indexOf('a',3));
//反向索引一個字符出現位置,從後面開始找,但是角標不會變
sop(str.lastIndexOf('a'));
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
2判斷:
字符串中是否包含某個子串:boolean contains(str)
因爲indexOf(str),如果str不存在於字符串中,那麼就返回-1,所以此方法也可以用於判斷是否包含某字符串:if(str.indexOf("abc") !=-1),此方法不止可以判斷,還可以獲取角標
字符串中是否有內容:boolean isEmpty()注意,此方法在jdk1.6之後出現原理就是判斷長度是否爲0
字符串是否以指定內容開頭:boolean startWith(str);
字符串是否以指定內容結尾 :boolean endsWith(str);
判斷字符串的內容是否相同:boolean equals(str);
判斷內容是否相同,並忽略大小寫: boolean equalsIgnoreCase();
class StringDemo
{
public static void main(String[] args)
{
String s="abc"; //s是一個類類型變量,“abc”是一個對象
String s1=new String("abc");
String s2="abc";
method_get();
method_if();
}
public static void method_if()
{
String str="StringDemo.java";
//判斷字符串是否以“String”開頭
sop(str.startsWith("String"));
//判斷字符串是否以“.java”結束
sop(str.endsWith(".java"));
//判斷字符串中是否包含“Demo”
sop(str.contains("Demo"));
}
public static void method_get()
{
String str="abcdegha";
//長度
sop(str.length()); //此處可以將int型的基本數據類型作爲Object對象傳入,是因爲JDK1.5版本之後出現的新特性:基本數據類型的提升,裝箱
//根據索引獲取字符
sop(str.charAt(3));
//根據字符獲取索引,如果沒有,返回-1
sop(str.indexOf('a',3));
//反向索引一個字符出現位置,從後面開始找,但是角標不會變
sop(str.lastIndexOf('a'));
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
3.轉換
將字符數組轉爲字符串:
構造函數:String(char[ ])
String(char [ ],int offset,int count):將字符數組中的一部分轉換爲字符串,offset表示偏移量,count表示要轉換的字符個數
static String :valueCopyOf(char[])
static String valueCopyOf(char[],int offset,int count)
static StringvalueOf(char[])
static String valueOf(char[],int offset,int count)
將字符串轉爲字符數組:
char[]: toCharArray(),括號中的對象就是字符串,相當於省略了this
將字節數組轉爲字符串:
String(byte[])
String(byte[],int offset,int count)
將字符串轉爲字節數組:
byte[] getBytes()
將基本數據類型轉爲字符串:
static String:valueOf(int)3+" ",String.valueOf(3),這兩種都是將數字3轉換爲字符串
static String valueOf(double)
static String valueOf(char[],int offset,int count)
注意:字符串和字節數組在轉換過程中可以指定編碼表
class StringDemo
{
public static void main(String[] args)
{
String s="abc"; //s是一個類類型變量,“abc”是一個對象
String s1=new String("abc");
String s2="abc";
method_get();
method_if();
method_change();
}
public static void method_change()
{
String str="abcde";
char[] ch={'h','e','l','l','o','y','o','u'};
//通過構造函數轉換字符數組爲字符串
String s=new String(ch);
String s1=new String(ch,5,3); //轉換字符數組中的指定的一些字符
char[] c=str.toCharArray();
for(int x=0;x<c.length;x++)
{
System.out.println(c[x]);
}
sop(s);
sop(s1);
}
public static void method_if()
{
String str="StringDemo.java";
//判斷字符串是否以“String”開頭
sop(str.startsWith("String"));
//判斷字符串是否以“.java”結束
sop(str.endsWith(".java"));
//判斷字符串中是否包含“Demo”
sop(str.contains("Demo"));
}
public static void method_get()
{
String str="abcdegha";
//長度
sop(str.length()); //此處可以將int型的基本數據類型作爲Object對象傳入,是因爲JDK1.5版本之後出現的新特性:基本數據類型的提升,裝箱
//根據索引獲取字符
sop(str.charAt(3));
//根據字符獲取索引,如果沒有,返回-1
sop(str.indexOf('a',3));
//反向索引一個字符出現位置,從後面開始找,但是角標不會變
sop(str.lastIndexOf('a'));
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
4.替換7.去除空格,比較
字符串練習:
1.模擬一個字符串的trim方法,去除字符串兩端的空格
思路:建立兩個頭尾指針,分別從頭尾開始檢測,當遇到了空格,指針進行自增自減,最後獲取兩個指針之間的內容,就是去除了兩端空格的字符串
class StringTest1
{
public static void main(String[] args)
{
String str=" mytrim ";
System.out.println(str);
System.out.println(mytrim(str));
}
public static String mytrim(String str)
{
int sta=0,end=str.length()-1; //建立兩個標誌位,分別從頭尾開始,
while(sta<=end && str.charAt(sta)==' ') //檢測到了頭部有空格,頭部指針自增
{
sta++;
}
while(sta<=end && str.charAt(end)==' ') //檢測到了尾部有空格,尾部指針自減
{
end--;
}
String s=str.substring(sta,end+1); //注意獲取子串方法是包含頭不包含尾,所以end需要+1來獲取最後一個有效字符串
return s;
}
}
2.將字符串中指定的內容進行反轉,如:“abcde” "adcbe"
思路:將字符串轉換爲字符數組,可以根據數組角標首尾交換位置,完成數組的反轉,然後再將轉換了的數組轉換爲字符串
class StringTest2
{
public static void main(String[] args)
{
String str="abcdefg";
System.out.println(ReverseString(str,2,4));
}
public static String ReverseString(String str,int sta,int end) //完成字符串反轉的功能
{
//將字符串轉換成字符數組
char[] ch=str.toCharArray();
//對數組進行反轉
reverse(ch,sta,end);
//將新字符數組轉換爲字符串
return String.valueOf(ch);
}
private static void reverse(char[] ch,int x,int y) //完成字符數組反轉的方法
{
for(int sta=x,end=y-1;sta<=end;sta++,end--) //end=y-1表示包含頭不包含尾
{
swap(ch,sta,end);
}
}
private static void swap(char[] ch,int x,int y) //調換位置的方法
{
char temp=ch[x];
ch[x]=ch[y];
ch[y]=temp;
}
}
練習3:獲取一個字符串在另一個字符串中出現的位置
思路:既要判斷字符串是否存在於母字符串中,又要獲取字符串在母串中的位置,使用indexOf(),使用計數器,獲取一次,計數器增加一次,然後母串從獲取到的位置重新找子串
class StringTest3
{
public static void main(String[] args)
{
String str="abbcdefbbhgblbb";
String key="bb";
System.out.println(getLocation_2(str,key));
}
public static int getLocation(String str,String key)
{
int index=0; //定義初始角標
int count=0; //定義計數器
while((index=str.indexOf(key,index))!=-1) //當indexOf的返回值不爲-1,則說明子串存在於母串中,並且同時獲取到了子串在母串中的角標
{
str=str.substring(index+key.length()); //母串不斷從新的角標處開始獲取
System.out.println(str);
count++;
}
return count;
}
public static int getLocation_2(String str,String key)
{
int index=0; //初始化角標
int count=0; //初始化計數器
while((index=str.indexOf(key,index))!=-1) //使用indexOf(str,int offset)方法,不斷的變換角標,當找到初始位置時,角標就從key存在於母串中之後的位置開始找
{
index=index+key.length(); //角標位從找到子串並超越子串之後的位置查找,這樣就不用在內存中不斷開闢空間存取新的字符串
System.out.println(index);
count++;
}
return count;
}
}
練習四:獲取兩個字符串的最大相同子串
思路:用短的字符串和長字符串作對比,如果不相同,短的字符串長度遞減,依舊和長字符串作對比,直到獲取最長相同子串爲止
class StringTest4
{
public static void main(String[] args)
{
String s1="abcwerthelloyuiodef";
String s2="zs";
sop(getMaxSbuString(s1,s2));
}
public static String getMaxSbuString(String s1,String s2)
{
//初始化兩個字符串,用於表示長短字符串
String max="";
String min="";
max=(s1.length()>s2.length())?s1:s2; //將長字符串賦給max
min=(max==s1)?s2:s1; //將短字符串賦給min
for(int x=0;x<min.length();x++) //外循環控制整個循環的次數,x=0的時候就是整個s2進行內循環
{
//定義每次與長字符串s2比較的子串的頭指針y,尾指針z,注意,並不包含z,而是子串到z-1,因爲z要取到s2的長度,這樣才能獲取到最後一個字符,所以z!=s2.length+1
for(int y=0,z=min.length()-x;z!=min.length()+1;y++,z++)
{
//用臨時字符串變量記錄每次獲取的s2的子串,好用來和長字符串作比較
String temp=min.substring(y,z);
if(max.contains(temp)) //判斷s1中是否包含子串
return temp;
}
}
return " "; //如果循環沒有找到相同字符串,就返回空的字符串
}
public static void sop(String str)
{
System.out.println(str);
}
}
StringBuffer
StringBuffer是字符緩衝區,是一個容器,長度可變,可以直接操作多個數據類型
最終可以通過toString()方法變成字符串
在這個容器中可以存儲,刪除,獲取,修改
存儲:StringBuffer append()將指定數據添加到已有數據的結尾處
StringBuffer insert(index,obj) 在指定位置添加元素
刪除:StringBuffer delete(int start,int end)
//包含頭不包含尾
StringBuffer deleteCharAt(index)
//刪除指定位置的字符
獲取:
char charAt(index) 獲取指定位置的字符
int indexOf(String str); 獲取字符串在StringBuffer中的位置
int lastIndexOf(String str) 反向獲取str在StringBuffer中的位置
int length() 獲取長度
String substring(int start,int end) //獲取指定位置的字符串
修改:
StringBuffer replace(int start,int end,String str) 將容器中指定位置之間的數據替換成str
void setCharAt(int index,char ch) 將指定位置的字符替換成新字符ch,注意此方法沒有明確的返回值,只要修改就可以了
反轉:
StringBuffer reverse();
class StringBufferDemo
{
public static void main(String[] args)
{
update_method();
}
public static void update_method()
{
StringBuffer sb=new StringBuffer("abcde");
sb.replace(0,3,"hello"); //將容器中的指定位置間的元素替換成指定元素
sop(sb.toString());
sb.setCharAt(1,'o'); //注意此方法沒有明確的返回值,只要修改了就好了
sop(sb.toString());
}
public static void delete_method()
{
StringBuffer sb=new StringBuffer("abcde");
//sb.delete(0,3); //包含頭不包含尾
sop(sb.toString());
//sb.delete(0,sb.length()); //清除緩衝區
sb.deleteCharAt(1); //刪除指定位置的字符
sop(sb.toString());
}
public static void add_method()
{
StringBuffer sb=new StringBuffer();
sb.append("abc"); //返回的還是StringBuffer對象
sb.append(34).append(true).append('b'); //方法調用鏈,因爲返回的仍然是本類對象
sop(sb.toString());
sb.insert(2,"hello"); //在2號角標位插入元素
sop(sb.toString());
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
JDK1.5版本之後,出現了StringBuilder,與StringBuffer功能相似,不同之處是StringBuffer是線程同步的,安全,而StringBuilder線程不同步
基本數據類型包裝類
其他數據類型轉換爲十進制:parseInt(String,radix),radix代表要轉換的進制,值爲2,8,16
十進制轉換爲其他進制:toHexString(),toBinaryString();
class IntDemo
{
public static void main(String[] args)
{
int x=Integer.parseInt("3c",16); //將3c轉換爲10十進制
System.out.println(x);
int y=Integer.parseInt("123"); //將字符串"123"轉換爲整數型123
System.out.println(y+4);
System.out.println(Integer.toHexString(12)); //將10進制轉換爲16進制
}
}
JDK1.5版本之後出現了新特性,基本數據類型的自動升級,自動裝箱
出現此新特性之後,Integer x=null.是成立的
class IntDemo1
{
public static void main(String[] args)
{
Integer x=4; //自動裝箱,4是一個對象,但是可以直接參與運算,相當於Integer x=new Integer(4)
int y=x+2; //進行自動拆箱,相當於x.intValue(x)+2,intValue()是獲取int型的數
Integer a=new Integer("123");
Integer b=new Integer(123);
System.out.println("a==b="+(a==b)); //結果爲false,因爲是新建了兩個新對象,所以不同
System.out.println("a.equals(b)="+a.equals(b)); //Integer複寫了equals方法,比較兩個對象中的數值是否相同
Integer c=128;
Integer d=128;
System.out.println("c==d="+(c==d)); //結果爲false,因爲相當於重建了兩個對象,指向不同
Integer e=127;
Integer f=127;
System.out.println("e==f="+(e==f)); //結果爲真,因爲這是JDK1.5版本之後的特點,值在byte範圍內,如果之前的對象存在,就不會新建對象,而指向之前的對象
}
}