1. 程式人生 > >JAVA語言中String類的常用方法

JAVA語言中String類的常用方法

String類的建立

String str = new String();// 建立
String str = new String("Hello World!");//初始化

"+"號運算子

既可以在String物件之間相加,也能加整形和浮點型數字(自動轉換為String物件)。

//示例
String s1 = new String("abc");
String s2 = new String("def");
String s3 = s1 + s2;//String物件相加
String s4 = s1 + 1;//String物件與整型相加
String s5 = s2 + 1.00;///String物件與浮點型相加(預設保留一位小數)
System.out.println(s3+"\n"+s4+"\n"+s5);

常用方法

求字串長度

public int length()

String str = new String("abcde");
System.out.println(str.length());//結果為5

求指定位置的字元

public char charAt(int index)

String str = new String("abcde");
System.out.println(str.charAt(2));//結果為c

提取子串

public int substring(int begin) //從begin到length()-1 public int substring(int begin, int end) //從begin到end-1

String str = new String("abcde");
System.out.println(str.substring(2));//結果為cde
System.out.println(str.substring(1,4));//結果為bcd

字串比較

public boolean equals(String anotherString) //判斷是否相等 public boolean equalsIgnoreCase(String anotherString) //判斷是否相等,忽略大小寫 public int compareTo(String anotherString)//比較,物件大則返回正數,等於則返回0,引數大則返回負數 public int compareToIgnoreCase(String anotherString)//比較,忽略大小寫

String str = new String("abcde");
System.out.println(str.equal("Abcde"));//結果為false
System.out.println(str.equalIgnoreCase("Abcde"));//結果為true
System.out.println(str.compareTo("Abcde"));//結果為正數
System.out.println(str.compareToIgnoreCase("Abcde"));//結果為0

字串連線(效果等同於"+")

public String concat(String str)

String str = new String("abcde");
System.out.println(str.concat("fg"));//結果為abcdefg

字串查詢

public int indexOf(String str)//找到子串在當前字串中從左邊起首次出現的位置,若沒有出現則返回-1。 public int indexOf(String str, int from)//與第一種類似,區別在於該方法從from位置向後查詢。 public int lastIndexOf(int ch/String str)//與第一種類似,區別在於該方法從字串的末尾位置向前查詢。 public int lastIndexOf(int ch/String str, int from)//與第二種方法類似,區別於該方法從from位置向前查詢。

String str = new String("abcdefgabcd");
System.out.println(str.indexOf("cd"));//結果為2
System.out.println(str.indexOf("cd", 3));//結果為9
System.out.println(str.lastIndexOf("cd"));//結果為9
System.out.println(str.lastIndexOf("cd", 7));//結果為2

大小寫轉換

public String toLowerCase()//全部轉小寫 public String toUpperCase()//全部轉大寫

String str = new String("abCdeFgaBcd");
System.out.println(str.toLowerCase());//結果為abcdefgabcd
System.out.println(str.toUpperCase());//結果為ABCDEFGABCD

字元(串)替換

public String replace(char old, char new)//當前物件中所有的old被替換為new public String replaceFirst(String oldstr, String newstr)//替換第一個oldstr為newstr public String replaceAll(String oldstr, String newstr)//替換所有的oldstr為newstr

String str = new String("abcdefgabcd");
System.out.println(str.replace('a', '*'));//結果為*bcdefg*bcd
System.out.println(str.replaceFirst("abcd", "1234"));//結果為1234efgabcd
System.out.println(str.replaceAll("abcd", "1234"));//結果為1234efg1234

其他方法

public String trim()// 截去字串兩端的空格,中間空格的不處理。

String str = new String(" a b c ");
System.out.println(str.trim());//結果為a b c

public String split(String str)// 將str內的所有字元作為分隔符進行字串分解。

String str = new String("ABC!JKLJIL$LJKJ%LJI$JJJ");
String[] result = str.split("!$%");
//result[0] = "ABC";
//result[1] = "JKLJIL";
//result[2] = "LJKJ";
//result[3] = "LJI";
//result[4] = "JJJ";

基本型別轉換

字串轉其他型別: public staric byte parseByte(String s) public staric short parseShort(String s) public staric int parseInt(String s) public staric long parseLong(String s) public staric float parseFloat(String s) public staric double parseDouble(String s)

int n = Integer.parseInt("12");
float f = Float.parseFloat("12.34");
double d = Double.parseDouble("1.124");

其他型別轉字串: public static String valueOf(type data) //type可以是其他任意常見型別

1 String s1 = String.valueOf(12);
2 String s1 = String.valueOf(12.34);