比如 一个 String str=“fffff” 怎么样用Java语句判断str中的字符都是f呢 ?各位老师 帮帮忙!!
 
谢谢

解决方案 »

  1.   

    用正则表达式吧String rgx = "f+";
    if(str.matches(rgx)){
    System.out.println("matched!");
    }
      

  2.   

    支持2楼,JAVA中的与正则表达式,很好用,很有效
    楼主可以看下JAVA的正则表达式实现
      

  3.   

    各位 可以详细点吗 比如indexOf 怎么用 ?
    我是个新手 很多都不会的 请各位 老师 不吝赐教  谢谢
      

  4.   

    我用subString给你做了下 和indexof差不多吧原理上
    public class test { public static void main(String[] args) {
    String str = "fffffff";
    int count = 0;
    for(int i =0;i<str.length();i++) {
    if(str.substring(i, i+1).equals("f")) {
    count++;
    if(count == str.length()) 
    System.out.println("字符串中字符都是f");
    }
    else
    System.out.println("不是预期的结果");
        }结果输出:
    字符串中字符都是f
      

  5.   

    public class test {    public static void main(String[] args) {
            String str = "fffffff";
                if(str.matches("^[f]+$")) {
                        System.out.println("字符串中字符都是f");
                }
                else{
                    System.out.println("不是预期的结果");
                }
            }
      

  6.   

    谢谢 九楼和七楼还有一楼 二楼 三楼的 师父 和老师的帮助
    现在正在研究 Java的正则表达式呢 
    indexOf 真的很强大我还有个问题为什么我这个程序就不能得到 正确的结果呢
    public class test {     public static void main(String[] args) { 
            String str = "fffffff"; 
            int j=0;
            char [] charArray = str.toCharArray();
            String s=String.valueOf(charArray[0]);    for(int i=0;i<charArray.length;i++){
             if(charArray[i-]==charArray[i+1]){
                  j++;
              }
            }
        if(j==charArray.length){
             System.out.println("字符串中字符都是f"); 
                } 
                else{ 
                    System.out.println("不是预期的结果"); 
                     }}
    请各位 老师 高手 赐教
      

  7.   

    charAt  indexOf  subString  应该都可以matches 很强大