Android(Java) 字符串的常用操作,获取指定字符出现的次数,根据指定字符截取字符串
/*这是第100000份数据,要截取出100000*/
String s="这是第100000份数据";
String s1 = s.substring(s.indexOf("第") + 1, s.indexOf("份"));
/*判断指定字符出现了几次*/
public static int countStr(String str, char key) { int count = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == key) count++; } return count == 0 ? -1 : count; }
/*根据指定字符截取字符串存入到数组中*/
private static String[] stringToArray(String str, String key) { String[] temp = null; temp = str.split(key); return temp; }
MainText.java
public static void main(String[] args) { String str = "abc;abck;abc"; // System.out.println(countStr(str, ';')); String [] temp = null; temp=stringToArray(str, ";"); for (int i = 0; i < temp.length; i++) { System.out.println(temp[i]); } }