1>獲取
1.1:字符串中包含的字符數(shù),也就是字符串的長度。
int length():獲取長度
1.2:根據(jù)位置獲取位置上某個(gè)字符。
char charAt(int index)
1.3:根據(jù)字符獲取該字符在字符串中的位置。
int indexOf(int ch):返回的是ch在字符串中第一次出現(xiàn)的位置。
int indexOf(int ch,int fromIndex):從fromIndex指定位置開始,獲取ch在字符串中出現(xiàn)的位置。
int indexOf(String str):返回的是str在字符串中第一次出現(xiàn)的位置。
int indexOf(String str,int fromIndex):從fromIndex指定位置開始,獲取str在字符串中出現(xiàn)的位置。
1.4:int lastIndexOf(String str):反向索引。
2>判斷
2.1:字符串中是否包含某一個(gè)子串。
boolean contains(str);
特殊之處:indexOf(str):可以索引str第一次出現(xiàn)為止,如果返回-1,表示該str不在字符串中存在。
所以,也可以用于對(duì)指定判斷是否包含。
if(str.indexOf("a")!=1)
而且該方法既可以判斷,也可以獲取出現(xiàn)的位置。
2.2:字符串中是否有內(nèi)容。
boolean isEmpty():原理就是判斷長度是否為0。
2.3:字符串是否以指定內(nèi)容開頭。
boolean startsWith(str);
2.4:字符串是否以指定內(nèi)容結(jié)尾。
boolean endsWith(str);
2.5:判斷字符內(nèi)容是否相同,復(fù)寫了object類中的equals方法。
boolean equals(str);
2.6:判斷內(nèi)容是否相同,并忽略大小寫。
boolean.equalsIgnorecase();
3>轉(zhuǎn)換
3.1:將字符數(shù)組轉(zhuǎn)成字符串。
構(gòu)造函數(shù):String(char[])
String(char[],offset,count):將字符數(shù)組中的一部分轉(zhuǎn)成字符串
靜態(tài)方法:
static String copyValueOf(char[]);
static String copyValueOf(char[] data,int offset,int count);
static String valueOf(char[]);
3.2:將字符串轉(zhuǎn)成字符組
char[] tocharArray();
3.3:將字節(jié)數(shù)組轉(zhuǎn)成字符串。
String(byte[])
String(byte[],offset,count):將字節(jié)數(shù)組中的一部分轉(zhuǎn)成字符串
3.4:將字符串轉(zhuǎn)成字節(jié)數(shù)組。
byte[] getBytes()
3.5:將基本數(shù)據(jù)類型轉(zhuǎn)成字符串,
static String valueOf(int)
static String valueOf(double)
// 3+"" 與 String.valueOf(3)的值是一樣的
特殊:字符串和字節(jié)數(shù)組在轉(zhuǎn)換過程中,是可以指定編碼的。
4>替換
String replace(oldchar,newchar);
5>切割
String[] split(regex);
6>子串。獲取字符串中的而一部分
String subString(begin);
String subString(begin,end);
7>轉(zhuǎn)換,去除空格,比較。
7.1:將字符串轉(zhuǎn)成大寫或小寫
String toUpperCsae() 大轉(zhuǎn)小
String toLowerCsae() 小轉(zhuǎn)大
7.2:將字符串兩端的多個(gè)空格去除
String trim();
7.3:對(duì)兩個(gè)字符串進(jìn)行自然順序的比較
int compareTo(string);
請(qǐng)看如下代碼,下面的代碼都是針對(duì)上面string七種用法而進(jìn)行一一舉例說明:
class StringMethodDemo
{
public static void method_Zhuanhuan_Qukong_Bijiao()
{
String s = " hello Java ";
//打印結(jié)果是:(hello和java前后門都有空格)hello java
sop(s.toUpperCase());
//打印結(jié)果是:(HELLO和JAVA前后門都有空格)HELLO JAVA
sop(s.toLowerCase());
//打印及結(jié)果是:不帶空格的“hello java”
sop(s.trim());
//比較數(shù)的大寫,打印結(jié)果是:1,因?yàn)閎對(duì)應(yīng)ascii值是98,
//a對(duì)應(yīng)是97,所以b-a=1
String s1 = "abc";
String s2 = "aaa";
sop(s1.compareTo(s2));
}
public static void method_sub()
{
String s = "abcdef";
//打印結(jié)果是:cdef,從指定位置開始到結(jié)尾。如果角標(biāo)不存在,會(huì)出現(xiàn)字符串角標(biāo)越界。
sop(s.substring(2));
//打印結(jié)果是:cd,包含頭,不包含尾。
sop(s.substring(2,4));
}
public static void method_split()
{
String s = "zhangsan,lisi,wangwu";
String[] arr = s.split(",");
for(int x=0; x<arr.length; x++)
{
sop(arr[x]);
}
}
public static void method_replace()
{
String s = "hello java";
//String s1 = s.replace('a','n');
//String s1 = s.replace('w','n'); 如果要替換的字符不存在,返回的還是原串
String s1 = s.replace("java","world");//打印結(jié)果是:hello world
sop("s="+s); //打印結(jié)果是:hello java因?yàn)樽址坏┍怀跏蓟稻筒豢杀桓淖?br /> sop("s1="+s1);//打印結(jié)果是:hello jnvn
}
public static void method_trans()
{
char[] arr = {'a','b','c','d','e','f'};
String s = new String(arr,1,3);
sop("s="+s);//打印結(jié)果是:bcd
String s1 = "zxcvbnm";
char[] chs = s1.toCharArray();
for(int x=0; x<chs.length; x++)
{
sop("ch="+chs[x]);//打印結(jié)果是:ch=z,x,c,v,b,n,m
}
}
public static void method_is()
{
String str = "ArrayDemo.java";
//判斷文件名稱是否是Array單詞開頭
sop(str.startsWith("Array"));
//判斷文件名稱是否是.java的文件
sop(str.endsWith(".java"));
//判斷文件中是否包含Demo
sop(str.contains("Demo"));
}
public static void method_get()
{
String str = "abcdeakpf";
//長度
sop(str.length());
//根據(jù)索引獲取字符
sop(str.charAt(4));
//sop(str.charAt(40));當(dāng)訪問到字符串中不存在的角標(biāo)時(shí)會(huì)發(fā)生StringIndexOutOfBoundsException(字符串角標(biāo)越界異常)
//根據(jù)字符獲取索引
//sop(str.indexOf('a'));
sop(str.indexOf('a',3));//打印的是5,因?yàn)榻菢?biāo)3是d,
//所以從d后面開始找a,第5個(gè)角標(biāo)是a
//sop(str.indexOf('t',3))打印:-1,如果沒有找到角標(biāo),返回-1
//反向索引一個(gè)字符出現(xiàn)的位置(從右往左查找,但是角標(biāo)還是從左開始)
sop(str.lastIndexOf("a"));
}
public static void main(String[] args)
{
method_Zhuanhuan_Qukong_Bijiao();
//method_sub();
//method_split();
//method_replace();
//method_trans();
//method_is();
//method_get();
/*
String s1 = "abc";
String s2 = new String("abc");
String s3 = "abc";
System.out.println(s1==s2);
System.out.println(s1==s3);
*/
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}