查看java字符串中某字符的个数;
比如 String str=1989-2-12;
想判断此字符串中“-”的个数;
有没有直接可用的方法。
求解。
:lol: 最终我解决了这个问题:
package action;
public class test {
public static void main (String[] args){
System.out.println(getMem("1989-2-12-21","-"));
}
public static int getMem(String str,String m ){
int i=0;
while(str.indexOf(m)!=-1){
int a=str.indexOf(m);
str=str.substring(a+1);
i++;
}
return i;
}
}
比如 String str=1989-2-12;
想判断此字符串中“-”的个数;
有没有直接可用的方法。
求解。
:lol: 最终我解决了这个问题:
package action;
public class test {
public static void main (String[] args){
System.out.println(getMem("1989-2-12-21","-"));
}
public static int getMem(String str,String m ){
int i=0;
while(str.indexOf(m)!=-1){
int a=str.indexOf(m);
str=str.substring(a+1);
i++;
}
return i;
}
}