——- android教育訓練、java教育訓練、期待與您交流! ———-
String類用來描述字元串,它提供了許多的方法來操作字元串。
那麼,在String衆多的方法中,常用的方法有哪些呢??
1 擷取
1.1 字元串的長度
int length();
示例:
String str = "abcdefg";
str.length();
注意不是
str.length;
1.2 根據索引擷取字元
char charAt(int index);
示例:
String str = "abcdefg";
char ch = str.charAt(1);
ch = ‘b’;
1.3 根據字元擷取索引
int indexOf(int ch);
注意參數不是
char ch
,因為字元使用的是ASCII碼。
int indexOf(int ch,int fromIndex);
從fromIndex指定位置開始,擷取字元在字元串中出現的位置。
示例:
String str = "abcdefg";
int index = str.indexOf('c');
index = 2;
String str = "abcdefgc";
int index = str.indexOf('c',3);
System.out.println(index);
index = 7;
1.4 根據字元串擷取索引
擷取字元串str在字元串String中第一次出現的位置
int indexOf(String str)
從fromIndex指定位置開始,擷取str在String字元串中第一次出現的位置
int indexOf(String str,int fromIndex)
示例:
String str = "abcdefgc";
int index = str.indexOf("def");
System.out.println(index);
```
index = 3;
String str = “abcdefgcdefdsadsdef”;
int index = str.indexOf(“def”,5);
System.out.println(index);
“`
index = 8;
1.5 反向索引
int lastIndexOf(int ch);
int lastIndexOf(int ch,int fromIndex);
int lastIndexOf(String str);
int lastIndexOf(String str,int fromIndex);
示例:
String str = "abcdefgcdefdsadsdef";
int index = str.lastIndexOf("def");
System.out.println(index);
index = 16;
String str = "abcdefgcdefdsadsdef";
int index = str.lastIndexOf("def",15);
System.out.println(index);
index = 9;
2 判斷
2.1 是否包含字串
boolean contains(String str)
For example:
String str1 = "abcdefgcdefdsadsdef";
String str2 = "abc";
String str3 = "dc";
System.out.println(str1.contains(str2));//true
System.out.println(str1.contains(str3));//false
int indexOf(String str);
特殊之處:indexOf(str):可以索引str第一次出現位置,如果傳回-1.表示該str不在字元串中存在。
是以,也可以用于對指定判斷是否包含。
if(str.indexOf(“aa”)!=-1)
而且該方法即可以判斷,有可以擷取出現的位置。
For example:
String str1 = "abcdefgcdefdsadsdef";
String str2 = "abc";
String str3 = "dc";
System.out.println(str1.indexOf(str2));//0
System.out.println(str1.indexOf(str3));//-1
2.2 是否有内容
boolean isEmpty();
原理就是判斷長度是否為0.
For example,
String str1 = "abcdefgcdefdsadsdef";
String str2 = "";
System.out.println(str1.isEmpty());//false
System.out.println(str2.isEmpty());//true
2.3 是否以指定字首開頭
boolean startsWith(String str)
For example,
String str1 = "abcdefgcdefdsadsdef";
System.out.println(str1.startsWith("abc"));//true
System.out.println(str1.startsWith("abd"));//false
2.4 是否以指定字尾結尾
boolean endsWith(String str)
For example.
String str1 = "abcdefgcdefdsadsdef";
System.out.println(str1.endsWith("def"));//true
System.out.println(str1.endsWith("sd"));//false
2.5 字元串内容是否相等
boolean equals(String str)
For example,
String str1 = "abcdefgcdefdsadsdef";
String str2 = new String("abcdefgcdefdsadsdef");
System.out.println(str1.equals(str2));//true
System.out.println(str1==str2);//false
2.6 忽略大小寫字元串内容是否相等
boolean equalsIgnoreCase(String str)
For example,
String str1 = "abcdefgcdefdsadsdef";
String str2 = new String("ABCDEFGCDEFDSADSDEF");
System.out.println(str1.equals(str2));//false
System.out.println(str1.equalsIgnoreCase(str2));//true
3 轉換
3.1 從字元數組轉換為字元串
3.1.1 構造函數法
将字元數組全部轉為字元串
String(char[] array)
将部分字元轉為字元串
For example,
char[] array = {'a','b','c','d','e','f','g','h','i','j'};
String str = new String(array);
System.out.println(str);
結果:abcdefghij
String(char[] array,int offset,int count)
For example,
char[] array = {'a','b','c','d','e','f','g','h','i','j'};
String str = new String(array,2,5);
System.out.println(str);
結果:cdefg
3.1.2 靜态方法
static String copyValueOf(char[] array)
For example,
char[] array = {'a','b','c','d','e','f','g','h','i','j'};
System.out.print(String.copyValueOf(array));
結果是:abcdefghij
static String copyValueOf(char[] array,int offset,int count)
For example,
char[] array = {'a','b','c','d','e','f','g','h','i','j'};
System.out.print(String.copyValueOf(array,2,5));
結果cdefg
3.2 從字元串轉換為字元數組
char[] toCharArray()
注意不要寫參數,因為是字元串對象調用該方法,參數就是this。
For example,
String str = new String("abcdefghjkl");
char[] array = str.toCharArray();
for(int i =0;i < array.length;i++)
System.out.print(array[i]+" ");
結果:a b c d e f g h j k l
3.3 從位元組數組轉換為字元串
String(byte[] array)
static String copyValueOf(byte[] array)
static String copyValueOf(byte array,int offset,int count)
3.4 從字元串轉換為位元組數組
byte getBytes();
3.5 從基本資料類型轉換為字元串
staitc String valueOf(int)
static String valueOf(double)
static String valueOf(float)
...
For example,
int x = 3;
int y = 4;
System.out.println("x+y="+x+y);
System.out.println(String.valueOf(x+y));
結果
x+y=34
7
//3+”“;等價于//String.valueOf(3);
特殊:字元串和位元組數組在轉換過程中,是可以指定編碼表的。
4 替換
`String replace(oldChar,newChar)’
For example,
String str1 = "abcdefg";
String str2 = str1.replace('a','m');
System.out.println(str1);
System.out.println(str2);
結果
abcdefg
mbcdefg
String str1 = "abcdefg";
String str2 = str1.replace("abc","mnj");
System.out.println(str1);
System.out.println(str2);
結果
abcdefg
mnjdefg
說明:字元串對象一旦被建立啊,就不會被改變,是以str1沒變
5 切割
String[] split(regex)
For example
String str1 = "ab,cde,fg,dsadsd,dsadeqwe,dsdsaq";
String[] array = str1.split(",");
for(int i =0 ;i< array.length;i++)
System.out.println(array[i]);
結果
ab
cde
fg
dsadsd
dsadeqwe
dsdsaq
6 字串
String substring(int begin)
注意不是subString
String substring(int begin,int end)
包含頭不包含尾
For example,
String str1 = "abcdefgdsadsddsadeqwedsdsaq";
String str2 = str1.substring(2);
String str3 = str1.substring(2,5);
System.out.println(str2);
System.out.println(str3);
結果
cdefgdsadsddsadeqwedsdsaq
cde
7 轉換大小寫,去除空格和比較
7.1 轉換為大寫
String toUpperCae()
7.2 轉換為小寫
String toLowerCase()
String str1 = "abcdefgdsadsddsadeqwedsdsaq";
String str2 = str1.toUpperCase();
String str3 = str2.toLowerCase();
System.out.println(str2);
System.out.println(str3);
結果
ABCDEFGDSADSDDSADEQWEDSDSAQ
abcdefgdsadsddsadeqwedsdsaq
7.3 去除字元串兩端空格
String trim()
String str1 = " Hello java ";
String str2 = str1.trim();
System.out.println(str1);
System.out.println(str2);
結果
Hello java
Hello java
說明:字元串對象一旦被建立,就不可以被改變
7.4 比較字元串内容的字典序大小
int compareTo(String str)
For exampple
String str1 = "abcdef";
String str2 = "aaaaaaa";
System.out.println(str1.compareTo(str2));
結果
1
說明:從0腳标開始比較,如果str1的字元大于str2,則傳回str1減去str2的值,為正數;如果傳回值為0,說明相等,繼續比較下一個腳标;
如果傳回值為負數,說明str1的字典序小于str2;
如果最後傳回值為0,說明str1和str2的字典序相等;
如果最後傳回值為正數,說明,str1的字典序大于str2.