String類適用於描述字符串事物。
那麼它就提供了多個方法對字符串進行操作。
常見的操作有哪些?
"abcd"
1,獲取。
1.1 字符串中的包含的字符數,也就是字符串的長度。
int length():獲取長度。
1.2 根據位置獲取位置上某個字符。
char charAt(int index):
1.3 根據字符獲取該字符在字符串中位置。
int indexOf(int ch):返回的是ch在字符串中第一次出現的位置。
int indexOf(int ch, int fromIndex) :從fromIndex指定位置開始,獲取ch在字符串中出現的位置。
int indexOf(String str):返回的是str在字符串中第一次出現的位置。
int indexOf(String str, int fromIndex) :從fromIndex指定位置開始,獲取str在字符串中出現的位置。
int lastIndexOf(int ch) :
2,判斷。
2.1 字符串中是否包含某一個子串。
boolean contains(str):
特殊之處:indexOf(str):可以索引str第一次出現位置,如果返回-1.表示該str不在字符串中存在。
所以,也可以用於對指定判斷是否包含。
if(str.indexOf("aa")!=-1)
而且該方法即可以判斷,有可以獲取出現的位置。
2.2 字符中是否有內容。
boolean isEmpty(): 原理就是判斷長度是否爲0.
2.3 字符串是否是以指定內容開頭。
boolean startsWith(str);
2.4 字符串是否是以指定內容結尾。
boolean endsWith(str);
2.5 判斷字符串內容是否相同。複寫了Object類中的equals方法。
boolean equals(str);
2.6 判斷內容是否相同,並忽略大小寫。
boolean equalsIgnoreCase();
3,轉換。
3.1 將字符數組轉成字符串。
構造函數:String(char[])
String(char[],offset,count):將字符數組中的一部分轉成字符串。
靜態方法:
static String copyValueOf(char[]);
static String copyValueOf(char[] data, int offset, int count)
static String valueOf(char[]):
3.2 將字符串轉成字符數組。**
char[] toCharArray():
3.3 將字節數組轉成字符串。
String(byte[])
String(byte[],offset,count):將字節數組中的一部分轉成字符串。
3.4 將字符串轉成字節數組。
byte[] getBytes():
3.5 將基本數據類型轉成字符串。
static String valueOf(int)
static String valueOf(double)
//3+"";//String.valueOf(3);
特殊:字符串和字節數組在轉換過程中,是可以指定編碼表的。
4,替換
String replace(oldchar,newchar);
5,切割
String[] split(regex);
6,子串。獲取字符串中的一部分。
String substring(begin);
String substring(begin,end);
7,轉換,去除空格,比較。
7.1 將字符串轉成大寫或則小寫。
String toUpperCase();
String toLowerCase();
7.2 將字符串兩端的多個空格去除。
String trim();
7.3 對兩個字符串進行自然順序的比較。
int compareTo(string);
*/
class StringMethodDemo
{
public static void method_7()
{
String s = " Hello Java ";
sop(s.toLowerCase());
sop(s.toUpperCase());
sop(s.trim());
String s1 = "a1c";
String s2 = "aaa";
sop(s1.compareTo(s2));
}
public static void method_sub()
{
String s = "abcdef";
sop(s.substring(2));//從指定位置開始到結尾。如果角標不存在,會出現字符串角標越界異常。
sop(s.substring(2,4));//包含頭,不包含尾。s.substring(0,s.length());
}
public static void method_split()
{
String s = "zhagnsa,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('q','n');//如果要替換的字符不存在,返回的還是原串。
String s1 = s.replace("java","world");
sop("s="+s);
sop("s1="+s1);
}
public static void method_trans()
{
char[] arr = {'a','b','c','d','e','f'};
String s= new String(arr,1,3);
sop("s="+s);
String s1 = "zxcvbnm";
char[] chs = s1.toCharArray();
for(int x=0; x<chs.length; x++)
{
sop("ch="+chs[x]);
}
}
public static void method_is()
{
String str = "ArrayDemo.java";
//判斷文件名稱是否是Array單詞開頭。
sop(str.startsWith("Array"));
//判斷文件名稱是否是.java的文件。
sop(str.endsWith(".java"));
//判斷文件中是否包含Demo
sop(str.contains(".java"));
}
public static void method_get()
{
String str = "abcdeakpf";
//長度
sop(str.length());
//根據索引獲取字符。
sop(str.charAt(4));//當訪問到字符串中不存在的角標時會發生StringIndexOutOfBoundsException。
//根據字符獲取索引
sop(str.indexOf('m',3));//如果沒有找到,返回-1.
//反向索引一個字符出現位置。
sop(str.lastIndexOf("a"));
}
public static void main(String[] args)
{
method_7();
// 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);
}
}