繁体   English   中英

检查字符串在符合条件的字符串中包含字符“ g”的次数

[英]check how many times string contains character 'g' in eligible string

我们怎样才能检查包含任何字符如何可以任何时间字符串....例如: 工程是一个字符串包含多少次完整的字符串“G”

我知道这是一个古老的问题,但是有一个选项没有得到回答,这很简单:

int count = string.length() - string.replaceAll("g","").length()

尝试这个

int count = StringUtils.countMatches("engineering", "e");

有关StringUtils的更多信息可以从以下问题中了解到: 如何在Java中使用StringUtils?

我会使用PatternMatcher

String string = "engineering";
Pattern pattern = Pattern.compile("([gG])"); //case insensitive, use [g] for only lower
Matcher matcher = pattern.matcher(string);
int count = 0;
while (matcher.find()) count++;

尽管Regex可以正常工作,但是在这里并不需要。 您可以简单地使用for-loop来维护字符count

您需要将字符串转换为char数组:-

    String str = "engineering";
    char toCheck = 'g';
    int count = 0;

    for (char ch: str.toCharArray()) { 
        if (ch == toCheck) {
            count++;
        }
    }
    System.out.println(count);

或者,您也可以不转换charArraycharArray :-

for (int i = 0; i < str.length(); i++) {
    if (str.charAt(i) == toCheck) {
        count++;
    }
}
String s = "engineering";
char c = 'g';
s.replaceAll("[^"+ c +"]", "").length();

使用正则表达式[g]查找字符并计算发现结果,如下所示:

    Pattern pattern = Pattern.compile("[g]");
    Matcher matcher = pattern.matcher("engineering");
    int countCharacter = 0;
    while(matcher.find()) {
        countCharacter++;
    }
    System.out.println(countCharacter);

如果需要不区分大小写的计数,请在模式中将正则表达式用作[gG]

使用org.apache.commons.lang3包来使用StringUtils类。 下载jar文件并将其放入Web应用程序的lib文件夹中。

int count = StringUtils.countMatches("engineering", "e");

这是一个非常老的问题,但这可能会对某人有所帮助(“ _”)

您可以只使用此代码

public static void main(String[] args){
    String mainString = "This is and that is he is and she is";
    //To find The "is" from the mainString
    String whatToFind = "is";
    int result = getCountOf(mainString, whatToFind);
    System.out.println(result);
}

public static int countMatches(String mainString, String whatToFind){
    String tempString = mainString.replaceAll(whatToFind, "");
    //this even work for on letter
    int times = (mainString.length()-tempString.length())/whatToFind.length();

    //times should be 4
    return times;
}

您可以尝试以下操作:

String str = "engineering";
int letterCount = 0;
int index = -1;
while((index = str.indexOf('g', index+1)) > 0)
    letterCount++;
System.out.println("Letter Count = " + letterCount);

您可以遍历它,并保留想要的字母数。

public class Program {
    public static int countAChars(String s) {
        int count = 0;
        for(char c : s.toCharArray()) {
            if('a' == c) {
               count++;
            }
        }
        return count;
    }
}

或者您可以使用StringUtils进行计数。

int count = StringUtils.countMatches("engineering", "e");

您可以尝试Java-8方式。 简单,简单且可读性强。

long countOfA = str.chars().filter(ch -> ch == 'g').count();

这是一个古老的问题,它在Java中,但我将在Python中回答。 这可能会有所帮助:

string = 'E75;Z;00001;'

a = string.split(';')

print(len(a)-1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM