完整例子如下,修改find可以查找不同的字符出现的次数:
public static void main(String[] args){
String str="aabbccabc";
char find='a';
int count=0;
for(int i=0;i
count++;
}
System.out.println(count);
}
package test;
/**
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
System.out.println(charAt("t1tt", 't'));
}
public static int charAt(String string, char a) {
// 直接使用单字符切割串,取子串长度减一
return string.split(a + "", -1).length - 1;
}
}
。。。这个函数返回字符串里某个位置的字符
"abcdefg".charAt(0); //a
"abcdefg".charAt(6); //g
挨个数呗 然后用Map纪录一下 有+1 没有=1
charAt
public char charAt(int index)返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。序列的第一个 char 值位于索引 0 处,第二个位于索引 1 处,依此类推,这类似于数组索引。
public class CharAt {
public static int CharMethod(String str,char i){
int count=0;
for(int j=0;j
count+=1;
}
}
return count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(CharAt.CharMethod("dfeee", 'e'));
}
}