標籤:color string acea dex 通過 style number 替換方法 pareto
java字串的功能可以說非常強大, 它的每一種方法也都很有用.
java字串中常用的有兩種字串類, 分別是String類和StringBuffer類.
Sting類
String類的對象是不可變的.
建立String
String()String(String str)String(char value[]) //用字元數組產生一個串對象String(char value[], int offset, int count) //用字元數組value的offset位開始的count個字元,建立一個字串對象
常用方法
int length()String toLowerCase()//返回當前串的小寫String toUpperCase()//返回當前串的大寫char[] toCharArray()//返回當前串的字串數組String trim()//刪除當前字串的前部和後部空格並返回新串
執行個體:
String str = new String(" Hello world ");str.length(); //返回str的長度為11str.toLowerCase();//將str轉換為小寫, " hello world "str.toUpperCase();//將str轉換為大寫, " HELLO WORLD "char[] strChar = str.toCharArray();//將str轉換為strChar字元數組str.trim();//刪除前後的空格, "Hello world"
比較性質的方法
boolean regionMatches(int toffset, String str, int ooffset, int len)//比較從本串的toffset開始的len個字元和str從ooffset開始的len個字元是否一致, 一致則返回true(可用來檢測字元換字串在當前串中出現的次數)boolean regionMatches(boolean IgnoreCase, int toffset, String str, int ooffset, int len)//同上, IgnoreCase決定是否忽略大小寫, IgnoreCase為true時忽略大小寫String concat(String str)//返回當前字串與str串聯接後的新串int compareTo(String str)//比較字串中相同位置的Unicode, 若兩串相等返回0, 否則當前串大於str返回比較字元的差值int compareToIgnoreCase(String str)//忽略大小寫比較, 同上boolean equals(Object anObj)//比較兩個對象的值是否相等.這裡比較兩個字串對象是否相等boolean equalsIgnoreCase(String anotherString)//忽略大小寫, 比較兩個字串對象是否相等boolean startsWith(String prefix[, int toffset])//判斷當前字串從toffset開支是否以參數prefix開頭, []中括弧表示可省略boolean endsWidth(string prefix[, int toffset])//判斷當前字串從toffset開始是否以參數prefix結尾
尋找方法
//字元ch尋找, 注意是字元int indexOf(int ch)//從前向後找第一個字元ch出現的位置, 未找到返回-1int indexOf(int ch, int fromIndex)//從fromIndex位置開始向後找第一個字元ch出現的位置, 未找到返回-1int lastIndexOf(int ch)//從後向前找第一個字元ch出現的位置, 未找到返回-1int lastIndexOf(int ch, int fromIndex)//從fromIndex位置開始前後找第一個字元ch出現的位置, 未找到返回-1//子串str尋找int indexOf(String str)//從當前字串開頭向後尋找子串str第一次出現的位置, 未找到返回-1int indexOf(String str, int fromIndex)//從當前字串的fromIndex位置向後尋找子串str第一次出現的位置, 未找到返回-1int lastIndexOf(String str)//從當前字串結尾向前尋找子串str第一次出現的位置, 未找到返回-1int lastIndexOf(String str, int fromIndex)//從當前字串的fromIndex位置向前尋找子串str第一次出現的位置, 未找到返回-1char charAt(int index)//返回當前字串index位置處的字元
替換方法
//替換String replace(char oldchar, char newchar)//將字串中所有oldcha字元r替換為newchar字元String replaceFirst(String regex, String replacement)//將字串中第一個與Regexregex匹配的子串用新串replacement替換String replaceAll(String regex, String replacement)//將字串中所有與Regexregex匹配的子串用新串replacement替換String substring(int start[, int end])//返回start到end-1返回的子串, 若省略end, 則為start到串尾.String[] split(String regex)//返回當前字串通過Regex分割的字串數組
其他方法
將數字化的字串轉換為基本類型
public static byte parseByte(String s) throws NumberFormatExceptionpublic static short parseShort(String s) throws NumberFormatExceptionpublic static short parseInt(String s) throws NumberFormatExceptionpublic static long parseLong(String s) throws NumberFormatExceptionpublic static float parseFloat(String s) throws NumberFormatExceptionpublic static double parseDouble(String s) throws NumberFormatException
用法舉例:
int a = Integer.parseInt(“23”);
其他類型轉換為字串
public static String valueOf(int n)public static String valueOf(char[] data)public static String valueOf(Object obj)public static String copyValueOf(char[] data)等同於valueOf(char[] data)
用法舉例:
String.valueOf(334);
StringBuffer類
StringBuffer()//建立空StringBuffer對象StringBuffer(int length)//建立一個長度為length的StringBuffer對象StringBuffer(String str)//建立一個str字串StringBuffer對象StringBuffer append(Object obj)//將對象obj添加到StringBuffer對象中StringBuffer insert(int position, Object obj)//將對象obj插入到StringBuffer對象中的position位置StringBuffer setCharAt(int position, char ch)//用字元ch替換StringBuffer對象中的position位置StringBuffer deleteCharAt(int position)//刪除position位置的字元StringBuffer replace(int start, int end, String str)//將StringBuffer對象中start到end-1的位置用字串str替換
【java】開發中常用字串方法