一、字符串
阿新 • • 發佈:2018-02-24
pre ins aps args str indexof alt -a 刪除
一、操縱字符串的方法
1.charAt(int index)方法
獲取指定位置的字符
package property; public class TestString{ public static void main(String[] args){ String str="abcdefgh"; char ch=str.charAt(3); System.out.println(ch);//d } }View Code
2.toCharArray()方法
獲取對應的字符數組
package property; publicView Codeclass TestString{ public static void main(String[] args){ String str="abcdefgh"; char[] ch=str.toCharArray();//獲取對應的字符數組 System.out.println(str.length()==cs.length);//true,數組有length屬性,String類型有length()方法 } }
3.subString()方法
截取子字符串
package property; public classView CodeTestString{ public static void main(String[] args){ String str="abcdefgh"; String subStr1=str.subString(3);//截取從第3個開始的字符串 System.out.println(subStr1);//defgh String subStr2=str.subString(3,5);//截取從第3個開始到第5-1=4位置的字符串也就是在區間[3,5)的 System.out.println(subStr2);//de } }
4.spilt()方法
根據分隔符進行分隔
package property; public class TestString{ public static void main(String[] args){ String str="abcde_fgh"; String subStr[]=str.spilt("_");//根據"_",得到兩個部分,一部分為abcde,一部分為fgh for(String sub:substr){ System.out.println(sub); } } }View Code
5.trim()
去掉首尾空格
package property; public class TestString{ public static void main(String[] args){ String str=" abcde_fgh "; System.out.println(str); //去掉首尾空格 System.out.println(str.trim()); } } }View Code
6.toLowerCase/toUpperCase
變小寫/變大寫
package property; public class TestString{ public static void main(String[] args){ String str="SabXcdefgh"; //全部變小寫 System.out.println(str.toLowerCase()); //全部變大寫 System.out.println(str.toUpperCase()); } } }View Code
7.indexOf()方法/contains()方法
判斷字符或者子字符串出現的位置
package property; public class TestString{ public static void main(String[] args){ String str="SabXcdefghcyu"; System.out.println(str.index(‘c‘));//字符c第一次出現的位置 System.out.println(str.lastIndexOf(‘c‘));//字符c最後一次出現的位置 System.out.println(str.indexOf(‘c‘,5));//從位置5開始,字符c第一次出現的位置 System.out.println(str.contains("fgh"));//是否包含字符串fgh } } }View Code
8.replaceAll()/replaceFirst()
替換所有/只替換第一個
package property; public class TestString{ public static void main(String[] args){ String str="蓋+倫,在進行了連續8次擊殺後,獲得了超神 的稱號"; String temp=str.replaceAll("擊殺","被擊殺")//將所有的擊殺,替換為被擊殺 temp=temp.replaceAll("超神","超鬼");//將所有的超神,替換為超鬼 System.out.println(temp); temp=str.replaceFirst("+","");//只替換第一個 System.out.println(temp); } } }View Code
9.比較是否同一個對象
package character; public class TestString { public static void main(String[] args) { String str1 = "the light"; String str2 = new String(str1); //==用於判斷是否是同一個字符串對象 System.out.println( str1 == str2); } }View Code
10.StringBuffer
是可變長的字符串
append/delete/insert/reverse:追加/刪除/插入/反轉
package property; public class TestString{ public static void main(String[] args){ String str="abcdefgh"; StringBuffer sb=new StringBuffer(str);//根據str創建一個StringBuffer對象 sb.append("Hello World");//在最後面追加 System.out.println(sb); sb.delete(4,10);//刪除4-10之間的字符串 System.out.println(sb); sb.insert(4,"love");//在4這個位置插入love System.out.println(sb); sb.reverse();//反轉 System.out.println(sb); } }View Code
11.為什麽StringBuffer可變長?
和String內部是一個字符數組一樣,StringBuffer也維護了一個字符數組,但是這個字符數組,留有多余的長度。
String和StringBuffer的性能區別?
使用String的"+"方式連接字符串,消耗時間比,StringBuffer追加連接的時間長
一、字符串