您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Java >> 怎么样判断一个字符串中的字符相同

怎么样判断一个字符串中的字符相同

来源:网络整理     时间:2016/8/10 19:15:05     关键词:

关于网友提出的“ 怎么样判断一个字符串中的字符相同”问题疑问,本网通过在网上对“ 怎么样判断一个字符串中的字符相同”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 怎么样判断一个字符串中的字符相同
描述:

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


解决方案1:

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

解决方案2:

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

解决方案3:

我用subString给你做了下 和indexof差不多吧原理上

public class test {
public static void main(String[] args) {
String str = "fffffff";
int count = 0;
for(int i =0;i<>
if(str.substring(i, i+1).equals("f")) {
count++;
if(count == str.length()) 
System.out.println("字符串中字符都是f");
}
else
System.out.println("不是预期的结果");
    }

结果输出:
字符串中字符都是f 解决方案4:

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

解决方案5:

循环判断 indexof()


以上介绍了“ 怎么样判断一个字符串中的字符相同”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3195785.html

相关图片

相关文章