public class UseMethodOfString { /** * @the use of String * @author victor */ public static void main(String[] args) { // TODO Auto-generated method stub char[] chars = new char[]{'a','b','c','d','e','f','g'}; // 構造字符串的幾種方式 String str = "abcdefg"; String str1 = new String(chars); String str2 = new String(chars,2,5); String str3 = new String(str2); System.out.println("字符串1:"+str); System.out.println("字符串2:"+str1); System.out.println("字符串3:"+str2); System.out.println("字符串4:"+str3); // 字符串的長度 System.out.println(str.length()); // 字符串的鏈接 System.out.println("four:" + 2 + 2); System.out.println("four:" + (2 + 2)); // toString()函數的用法 Stu student1 = new Stu("victor",22,100); System.out.println(student1); // 提取字符 一、提取單個字符 char ch = str.charAt(0); System.out.println(ch); // 二、一次提取多個字符 int begin = 2; int end = 5; char []buff = new char[end-begin]; str.getChars(begin, end, buff, 0); System.out.println(buff); // 三、將String對象的全部對象轉化爲字符數組 char []value1 = str.toCharArray(); System.out.println(value1); // 比較字符串一、equals和equalsIgnoreCase if(str.equals(new String(value1))) System.out.println(str+ "equals" + new String(value1)); else System.out.println(str+ "not equals" +new String(value1)); // 二、reginMatch比較字符串中一個特定部分與另外一個字符串的特定部分,有一個重載的函數能夠忽略大小寫 // boolean reginMatches((boolean ignoreCase),int startIndex,String str2,int str2StartIndex,int numChars) if(str.regionMatches(2, str2, 0, 5)) System.out.println("regin equals"); else System.out.println("not equals"); // 三、startsWith肯定字符串是否以指定字符串開始,endsWith肯定字符串是不是以指定字符串結束 if(str.startsWith("b", 1)) System.out.println("yes!"); else System.out.println("no!"); if(str.endsWith(str2)) System.out.println("yes!"); else System.out.println("no!"); // 四、equals比較String對象中的字符,==比較String中的引用 System.out.println(str2+"equals"+str3+":"+str2.equals(str3)); System.out.println(str2+"=="+str3+":"+(str2==str3)); // 五、compareTo(String str),調用字符串小於str返回值小於0,大於str返回值大於0,等於str返回值等於0 if(str1.compareTo(str2)<0) System.out.println(str1+" littler than "+str2); else if(str1.compareTo(str2)==0) System.out.println(str1+" equals "+str2); if(str1.compareTo(str2)>0) System.out.println(str1+" bigger than "+str2); // 查找字符串indexOf和lastIndexOf,返回-1表示查找失敗 // int last/indexOf(int ch,int startIndex) System.out.println("aaaabbbcccbbb".indexOf('a',1 )); System.out.println("aaaabbbcccbbb".lastIndexOf('a',1 )); // 修改字符串一、subString提取指定範圍的字符串。兩種形式:String substring(int startIndex)或String substring(int startIndex,int endIndex) StrDemo strDemo = new StrDemo(); strDemo.demo(); // concat能夠用來鏈接兩個字符串,它的做用等效於+。String concat(String str) // replace使用一個字符或字符序列代替另外一個字符或字符序列String replace(char old,char new) // trim移除字符串開始和末尾的空格 // valueOf和toString的區別 System.out.println(String.valueOf(chars)); System.out.println(chars.toString()); System.out.println(String.valueOf(1.0000)); System.out.println(String.valueOf(1.00)); // 改變字符串的大小寫toLowerCase和toUpperCase // contains(string str)調用字符串包含str就返回true不然就返回false // boolean isEmpty()調用字符串沒有包含任何字符串且長度爲0就返回true不然就返回false,若是一個字符串值爲null,那麼他不能調用length方法 // boolean matches(String regExp)若是調用字符串和傳遞的正則表達式regExp相匹配就返回true // String replaceAll(String regExp,String newStr)使用newStr替換與regExp所指定的正則表達式匹配的全部子串 } } class Stu{ String name; int age; int score; public Stu(String name,int age,int score){ this.name = name; this.age = age; this.score = score; } public String toString(){ return "The student name is:" + name +" "+ "age is" + age +" "+ "score is" + score; } } class StrDemo{//把一個字符串的某個字符替換成另外一個字符 public void demo(){ String str1 = "This is a demo"; String str2 = "is"; String str3 = "was"; int i = str1.indexOf(str2); String result = null; while(i!=-1){ result = str1.substring(0, i); result+=str3; result+=str1.substring(i+str2.length(), str1.length()); str1 = result; i = str1.indexOf(str2); } System.out.println(result); } } 輸出結果爲: 字符串1:abcdefg 字符串2:abcdefg 字符串3:cdefg 字符串4:cdefg 7 four:22 four:4 The student name is:victor age is22 score is100 a cde abcdefg abcdefgequalsabcdefg regin equals yes! yes! cdefgequalscdefg:true cdefg==cdefg:false abcdefg littler than cdefg 1 1 Thwas was a demo abcdefg [C@d9f9c3 1.0 1.0