<span style="font-family: Arial, Helvetica, sans-serif;">我們都知道,String類是在java程序開發過程中使用比較多的類,必須將其完全掌握並熟練運用才行。基於這個原因,作者打算把String類中常用的方法進行總結 ,方便大家學習。</span>
1.字符串與字符數組
序號 |
類型 |
名稱 |
說明 |
1 |
構造 |
public String(char[] value) |
把字符數組轉化爲字符串 |
2 |
構造 |
public String(char[] value, int offset, int count) |
從offset開始,將count個數組轉化爲字符串 |
3 |
方法 |
public char charAt(int index) |
返回制定位置的字符 |
4 |
方法 |
public char[] toCharArray() |
把字符串轉化爲數組 |
案例:
public class StringTest
{
public static void main(String[] args)
{
char ch[]={'a','b','c'}; //定義原始的字符數組
char ch2[]= null; //字符串轉化之後保存數組
String str1=new String(ch); //全部轉化
String str2=new String(ch,1,2); //從1開始,轉化2個
System.out.println(str1);
System.out.println(str2);
System.out.println(str1.charAt(1)); //str1的搜索指定位置的字符
ch2= str1.toCharArray(); //str1字符串轉化爲字符數組
for (int i=0;i<ch2.length ;++i )
{
System.out.println(ch2[i]);
}
}
}
結果:
2.字符串與字節數組
序號 |
類型 |
名稱 |
說明 |
1 |
構造 |
public String(byte[] value) |
把字節數組轉化爲字符串 |
2 |
構造 |
public String(byte[] value, int offset, int count) |
從offset開始,將count個數組轉化爲字符串 |
3 |
方法 |
public byte[] getBytes() |
把字符串轉化爲字符數組 |
案例:
public class StringTest
{
public static void main(String[] args)
{
String str="hello world";<span style="white-space:pre"> </span>//定義字符串
byte b[]= str.getBytes();<span style="white-space:pre"> </span>//字符串轉換爲字節
System.out.println(new String(b));
}
}
結果
注意:在IO操作上經常會使用字符串和字節之間的轉換。
3.字符串的比較
序號 |
類型 |
名稱 |
說明 |
1 |
方法 |
public boolean equals(String str) |
比較兩個字符串是否相等,與大小寫有關 |
2 |
方法 |
public boolean equalsIgnoreCase(String str) |
比較兩個字符串是否相等,與大小寫無關 |
3 |
方法 |
public int compareTo(String str) |
返回值得到相等(0),大於(>0),小於(<0) |
程序
class StringTest
{
public static void main(String[] args)
{
String str1="hello world!";
String str2="HELLO WORLD!";
System.out.println("相等判斷(區分大小寫)");
System.out.println(str1.equals(str2));
System.out.println("相等判斷(不區分大小寫)");
System.out.println(str1.equalsIgnoreCase(str2));
System.out.println("比較大小");
System.out.println(str1.compareTo(str2));
}
}
結果
4.查找字符串
序號 |
類型 |
名稱 |
說明 |
1 |
方法 |
public boolean contains(String str) |
判斷是否存在字符串str |
2 |
方法 |
public int indexOf(String str) |
返回查找到字符串的位置 |
3 |
方法 |
public int indexOf(String str, int indexfrom) |
從indexfrom之後開始查找 |
4 |
方法 |
public int lastIndexOf(String str) |
從後向前找 |
5 |
方法 |
public int lastIndexOf(String str, int indexfrom) |
從後向前找,從指定位置 |
程序
class StringTest
{
public static void main(String[] args)
{
String str1="hello Tyrion!";
String str2="el";
System.out.println("搜索是否包含:");
System.out.println(str1.contains(str2));
System.out.println(str1.indexOf(str2));
System.out.println(str1.indexOf(str2,4));
System.out.println(str1.lastIndexOf(str2));
System.out.println(str1.lastIndexOf(str2,2));
}
}
結果
5.判斷開頭結尾
序號 |
類型 |
名稱 |
說明 |
1 |
方法 |
public boolean startWith(String str) |
判斷是否以字符串str開頭 |
2 |
方法 |
public boolean endWith(String str) |
判斷是否以字符串str結尾 |
程序
class StringTest
{
public static void main(String[] args)
{
String den="hello world";
String sta="he";
String end="ld";
if (den.startsWith(sta))
{
System.out.println("是指定的開頭");
}
if (den.endsWith(end))
{
System.out.println("是指定的結尾");
}
}
}
結果
6.字符串截取
序號 |
類型 |
名稱 |
說明 |
1 |
方法 |
public String substring(int index) |
從index位置開始截取剩餘字符串 |
2 |
方法 |
public String substring(int i, int j) |
從i 到j 之間的字符串進行截取 |
程序
class StringTest
{
public static void main(String[] args)
{
String den="hello world";
String str=null;
str=den.substring(6);
System.out.println(str);
str=den.substring(2,6);
System.out.println(str);
}
}
結果
7.字符串替換
序號 |
類型 |
名稱 |
說明 |
1 |
方法 |
public String replaceAll(String exp, String replace) |
把exp全部用replace替換 |
2 |
方法 |
public String replaceFirst(String exp, String replace) |
把第一個exp用replace替換 |
程序
class StringTest
{
public static void main(String[] args)
{
String den="hello world hello";
String str="hello";
System.out.println(den.replaceAll(str,"nihao"));
System.out.println(den.replaceFirst(str,"nihao"));
}
}
結果
7.字符串拆分
序號 |
類型 |
名稱 |
說明 |
1 |
方法 |
public String[] split(String exp) |
把exp進行拆分,得到String數組 |
2 |
方法 |
public String[] split(String exp, int limit) |
把exp進行拆分制定limit個,得到String數組 |
程序class StringTest
{
public static void main(String[] args)
{
String str="hello world";
String out[]=str.split("o");
for (int i=0;i<out.length ;++i )
{
System.out.println(out[i]);
}
String out2[]=str.split("o",2);
for (int i=0;i<out2.length ;++i )
{
System.out.println(out2[i]);
}
}
}
結果
注意:拆分之後的數組個數滿足要求個數之後,剩餘的不要拆分。如果有IP地址,以'.'作爲拆分表達式,此時拆不開。‘.’是正則規則的符號,要拆分必須使用轉義符\\. 拆不開的都用轉義符。
8.其他
序號 |
類型 |
名稱 |
說明 |
1 |
方法 |
public String toUpperCase() |
轉換成大寫 |
2 |
方法 |
public String toLowerCase() |
小寫 |
3 |
方法 |
public String trim() |
去掉左右空格 |
4 |
方法 |
public int length() |
長度 |
5 |
方法 |
public boolean isEmpty() |
判斷字符串是否是空 |
程序class StringTest
{
public static void main(String[] args)
{
String str=" hello WORLD ";
System.out.println("轉大寫");
System.out.println(str.toUpperCase());
System.out.println("轉小寫");
System.out.println((str.toUpperCase()).toLowerCase());
System.out.println("去除空格");
System.out.println(str.trim());
System.out.println("是否爲空:");
System.out.println(str.isEmpty());
}
}
結果
把字符串的首字母大寫
class StringTest
{
public static void main(String[] args)
{
String s="hello";
System.out.println(initcap(s));
}
public static String initcap(String str)
{
return str.substring(0,1).toUpperCase()+ str.substring(1);
}
}
結果
經典題目:輸入如下字符串:張三:11|李四:22|王五:33
使用String類進行分類,顯示如圖:
class StringTest
{
public static void main(String[] args)
{
String str="張三111|李四122|王五133";
int mid =0; //:的標誌
int end =0; //:成績結束的標誌
int start =0; //姓名開始的標誌
mid = str.indexOf(":");
end =str.indexOf("|",mid);
start = str.lastIndexOf("|",mid);
if(mid ==-1)
{
System.out.println("數據格式不對!");
}
while (mid !=-1) //存在:則向前找|作爲開始的標誌
{
if (start ==-1) //說明|存在,這個名字是第一個姓名
{
System.out.println( "姓名:" + str.substring(0,mid) + ", " + "成績" +
str.substring(mid,end) + "\n");
}
else if(end ==-1) //start不是-1說明姓名不是第一個,但是此時還要判斷是否結束-1則結束
{
System.out.println( "姓名:" + str.substring(start,mid) + ", " + "成績" +
str.substring(mid,str.length()) + "\n");
}
else //說明是中間的
{
System.out.println( "姓名:" + str.substring(start,mid) + ", " + "成績" +
str.substring(mid,end) + "\n");
}
mid = str.indexOf(":",mid+1);//繼續向後尋找,直至-1
end =str.indexOf("|",mid);
start = str.lastIndexOf("|",mid);
}
}
}
方法2:使用分割的方式
class StringTest
{
public static void main(String[] args)
{
String str ="張三:11|李四:22|王五:33";
String s[] = str.split("\\|");
for (int i =0;i<s.length ; ++i)
{
String temp[] =s[i].split(":");
System.out.println("姓名:" + temp[0] + ", " + "成績:"+ temp[1]);
}
}
}
總結:一定要掌握所有的這些方法。