/* 計算某字元出現在字串中幾次
* Para 1.String 主字串
* 2.String 要搜尋的字元
* out 1.int 出現次數
* */
public int numberOfKeywords(String strText,String keyword){
int count = 0;
String strTmp="";
for(int i=0;i<strText.length();i++){
strTmp= String.valueOf(strText.charAt(i));
//System.out.println(i+":"+strTmp);
if(keyword.equals(strTmp)){
count++;
}
}
return count;
}

/*取現在時間
* out 1.String 現在時間(格式為:2014/09/20 07:51:01)
*/
public String getNowDate(){
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
return sdf.format(c.getTime());
}

/* 將字串已^切割
    para 1.String 欲切割字串

    out   1.String[] 切割完畢的字串陣列

*/

public static String[] split(String data)
throws Exception {

List<Integer> tokenIndex = new ArrayList<Integer>();
for (int i = 0; i < data.length(); i++) {
if ("^".equals(String.valueOf(data.charAt(i))))
tokenIndex.add(new Integer(i));
}
tokenIndex.add(new Integer(data.length()));

List<String> dataList = new ArrayList<String>();
for (int i = 0; i < tokenIndex.size(); i++) {
String str = "";
int idx = ((Integer) tokenIndex.get(i)).intValue();
if (i == 0) {
str = data.substring(0, idx);
} else {
int prevIdx = ((Integer) tokenIndex.get(i - 1)).intValue();
str = data.substring(prevIdx + 1, idx);
}

dataList.add(str);
}

String[] dataArr = new String[dataList.size()];
for (int i = 0; i < dataList.size(); i++) {
dataArr[i] = dataList.get(i).toString();
}

return dataArr;
}

創作者介紹
創作者 Bear-小小工程師學習筆記 的頭像
carefree55

Bear-小小工程師學習筆記

carefree55 發表在 痞客邦 留言(0) 人氣( 48 )