- 字符串比較
字符串函數 compareTo (string) ,compareToIgnoreCase(String) 及 compareTo(object string) 來比較兩個字符串,並返回字符串中第一個字母ASCII的差值。
public class StringCompareEmp{ public static void main(String args[]){ String str = "Hello World"; String anotherString = "hello world"; Object objStr = str; System.out.println( str.compareTo(anotherString) ); System.out.println( str.compareToIgnoreCase(anotherString) ); //忽略大小寫 System.out.println( str.compareTo(objStr.toString())); } }
- 字符串查找
String 類的 indexOf() 方法在字符串中查找子字符串出現的位置,如過存在返回字符串出現的位置(第一位為0),如果不存在返回 -1。
public class SearchStringEmp { public static void main(String[] args) { String strOrig = "Google Java Taobao"; int intIndex = strOrig.indexOf("Java"); if(intIndex == - 1){ System.out.println("沒有找到字符串 Java"); }else{ System.out.println("Java 字符串位置 " + intIndex); } } }
- 查找字符串最后一次出現的位置
字符串函數 strOrig.lastIndexOf(Stringname) 來查找子字符串 Stringname 在 strOrig 出現的位置。
public class SearchlastString { public static void main(String[] args) { String strOrig = "Hello world ,Hello Runoob"; int lastIndex = strOrig.lastIndexOf("Hello"); if(lastIndex == - 1){ System.out.println("沒有找到字符串"); }else{ System.out.println("字符串最后出現的位置: "+ lastIndex); } } }
- 刪除字符串中的一個字符
通過字符串函數 substring() 函數來刪除字符串中的一個字符,我們將功能封裝在 removeCharAt 函數中。
public class Main { public static void main(String args[]) { String str = "this is Java"; System.out.println(removeCharAt(str, 3)); } public static String removeCharAt(String s, int pos) { return s.substring(0, pos) + s.substring(pos + 1); } }
- 字符串替換
java String 類的 replace 方法可以替換字符串中的字符。
public class test { public static void main(String args[]){ String str="Hello World,Hello Java."; System.out.println(str.replace('H','W')); //替換全部 System.out.println(str.replaceFirst("He","Wa")); //替換第一個遇到的 System.out.println(str.replaceAll("He", "Ha")); //替換全部 } }
- 字符串反轉
Java 的反轉函數 reverse() 可字符串反轉。
public class test { public static void main(String args[]){ String str="Hello Java."; String reverse = new StringBuffer(str).reverse().toString(); System.out.println("字符串反轉前:"+str); System.out.println("字符串反轉后:"+reverse); } }
- 字符串分割
split(string) 方法通過指定分隔符將字符串分割為數組。
public class test { public static void main(String args[]){ String str="www-baidu-com"; String[] temp; String delimeter = "-"; //指定分隔符 temp = str.split(delimeter); //分割字符串 //普通for循環 for(int i =0; i < temp.length; i++){ System.out.println(temp[i]); System.out.println(""); } System.out.println("----java for each循環輸出的方法-----"); String str1 = "www.baidu.com"; String[] temp1; String delimeter1 = "\\."; //指定分隔符,.號需要轉義 temp1 = str1.split(delimeter1); for (String x : temp1){ System.out.println(x); System.out.println(""); } } }
- 字符串小寫轉大寫
String toUpperCase() 方法將字符串從小寫轉為大寫。
String str = "string runoob";
String strUpper = str.toUpperCase();
- 測試兩個字符串區域是否相等
public class StringRegionMatch{ public static void main(String[] args){ String first_str = "Welcome to Microsoft"; String second_str = "I work with microsoft"; boolean match1 = first_str. regionMatches(11, second_str, 12, 9); boolean match2 = first_str. regionMatches(true, 11, second_str, 12, 9); //第一個參數 true 表示忽略大小寫區別 System.out.println("區分大小寫返回值:" + match1); System.out.println("不區分大小寫返回值:" + match2); } }
first_str.regionMatches(11, second_str, 12, 9) 表示將 first_str 字符串從第11個字符"M"開始和 second_str 字符串的第12個字符"M"開始逐個比較,共比較 9 對字符,由於字符串區分大小寫,所以結果為false。
如果設置第一個參數為 true ,則表示忽略大小寫區別,所以返回 true。
- 連接字符串
通過 "+" 操作符和StringBuffer.append() 方法來連接字符串。
public class StringConcatenate { public static void main(String[] args){ long startTime = System.currentTimeMillis(); for(int i=0;i<5000;i++){ String result = "This is" + "testing the" + "difference"+ "between" + "String"+ "and"+ "StringBuffer"; } long endTime = System.currentTimeMillis(); System.out.println("字符串連接" + " - 使用 + 操作符 : " + (endTime - startTime)+ " ms"); long startTime1 = System.currentTimeMillis(); for(int i=0;i<5000;i++){ StringBuffer result = new StringBuffer(); result.append("This is"); result.append("testing the"); result.append("difference"); result.append("between"); result.append("String"); result.append("and"); result.append("StringBuffer"); } long endTime1 = System.currentTimeMillis(); System.out.println("字符串連接" + " - 使用 StringBuffer : " + (endTime1 - startTime1)+ " ms"); } }
輸出:
字符串連接 - 使用 + 操作符 : 0 ms
字符串連接 - 使用 StringBuffer : 42 ms
