import java.util.Date;
public class Query {
public static void main(String[] args) {
String str="aasdsadasdff";
System.out.println(str.indexOf("b", 2));
boolean[] isCount=new boolean[str.length()];
char c=(char)0;
int max=0;
for(int i=0;i<str.length();i++){
if(!isCount[i]){
char temp=str.charAt(i);
int tempCount=1;
int tempIndex=i+1;
isCount[i]=true;
while(tempIndex<str.length()&&(tempIndex=str.indexOf(temp,tempIndex))>0){
tempCount++;
isCount[tempIndex]=true;
tempIndex++;
}
if(tempCount>max){
c=temp;
max=tempCount;
}
}
}
System.out.println("出现次数最多的字符是:"+c);
System.out.println("出现的次数是:"+max);
}
}
第二种
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 查找一个字符串出现最多字母的次数
* @author admin
*
*/
public class TestQuery {
String charData;
int max;
public static void main(String[] args) {
String str="aasdsadasdff";
TestQuery t = new TestQuery();
t.getMax(str);
System.out.println("字母"+t.charData+"---次数"+t.max);
}
void getMax(String str){
char[] c = str.toCharArray();
for (int i = 0; i < c.length; i++) {
int temp=0;
Pattern p = Pattern.compile(String.valueOf(c[i]));
Matcher m = p.matcher(str);
while (m.find()) {
temp++;
}
if(temp>max){
max=temp;
charData = String.valueOf(c[i]);
}
}
}
}
“`