Android String字串處理

記得之前寫過一篇"Object C - String",會寫這些很簡單就是因為我大腦記不住,反正知道去哪找就好,超小的腦容量要記住這麼多東西真是有點困難。

這邊就紀錄一下常用的合併、切割、型態轉換、搜尋、替換、找出位置...等

合併
/* 合併 */
String cht = "一二三四五六七八九十";
String num = "1234567890";
String append = cht+num;
System.out.println("append = "+append);
//一二三四五六七八九十1234567890


切割
/* 切割字串 */
String eng = "abcdefghijklmnopqrstuvwxyz";
String[] array = eng.split("k");
for(int i = 0 ; i < array.length ; i ++){ System.out.println("array["+i+"] = "+array[i]); } //array[0] = abcdefghij //array[1] = lmnopqrstuvwxyz


型態轉換
/* 轉換型態 */
String num = "1234567890";
//string轉int
int number = Integer.valueOf(num); //string to int
int aaa = 9999999;
//int轉string
String fromInt = String.valueOf(aaa);
System.out.println("fromInt = "+fromInt+" || number = "+number);
//fromInt = 9999999 || number = 1234567890


搜尋字串(從0開始算)
/* 搜尋字串 */
String eng = "abcdefghijklmnopqrstuvwxyz";
int searchLoc = eng.indexOf("i");
System.out.println("i = "+searchLoc);
//i = 8

searchLoc = eng.indexOf("xy");
System.out.println("xy = "+searchLoc);
//xy = 23

searchLoc = eng.indexOf("isken");
System.out.println("isken = "+searchLoc);
//isken = -1(找不到)


搜尋字元(從0開始算)
/* 搜尋字元 */
String cht = "一二三四五六七八九十";
String num = "1234567890";
char c = cht.charAt(2);
System.out.println("cht third char = "+c);
//cht third char = 三

c = eng.charAt(1);
System.out.println("eng second char = "+c);
//eng second char = b


取得字串指定位置(從0開始算)
/* 取得字串指定位置 */
String num = "1234567890";
String subTest = num.substring(1, 4);
System.out.println("subTest = "+subTest);
//subTest = 234


字串長度
/* 字串長度 */
System.out.println("cht length = "+cht.length());
//cht length = 10


替換字串
/* 替換字串 */
//替換全部
String replaceTest = "abcabcabc";
String replaced = replaceTest.replaceAll("abc", "123");
System.out.println("replaceAll = "+replaced);
//replaceAll = 123123123

//替換最先找到那個
replaced = replaceTest.replaceFirst("abc", "123");
System.out.println("replaceFirst = "+replaced);
//replaceFirst = 123abcabc


格式化字串
/* 格式化字串 */
String formatTest = "%02d";//定義為兩位不足兩位則補"0"
for(int i = 5; i < 15; i++){ System.out.println("format = "+String.format(formatTest, i)); } //format = 05 //format = 06 //... //format = 13 //format = 14


清除空白字串(清除前後所有的空白,但是字串與字串間空格不會被清除)
/* 清除字串空格 */
String trimTest1 = "abc ";
String trimTest2 = " abc";
String trimTest3 = " abc ";
String trimTest4 = "abc abc";
String trimTest5 = " d abc";
System.out.println("trimTest1 = "+trimTest1.trim());
System.out.println("trimTest2 = "+trimTest2.trim());
System.out.println("trimTest3 = "+trimTest3.trim());
System.out.println("trimTest4 = "+trimTest4.trim());
System.out.println("trimTest5 = "+trimTest5.trim());
//trimTest1 = abc
//trimTest2 = abc
//trimTest3 = abc
//trimTest4 = abc abc
//trimTest5 = d abc


字串比對
/* 字串比對 */
String cht = "一二三四五六七八九十";
String num = "1234567890";
String equalcht = "一二三四五六七八九十";
System.out.println("cht & equalcht = "+cht.equals(equalcht)+" || cht & eng = "+cht.equals(eng));
//print cht & equalcht = true || cht & eng = false


字串轉字元
/* 字串轉字元 */
String cht = "一二三四五六七八九十";
char c = cht.toCharArray()";
for(int i = 0; i < c.length; i++){ System.out.print("["+i+"] = "+c[i]); }


相關連結
Android Developer
Object C - String

1 則留言: