如何,我们可以检查包含任何字符如何可以任何时间串.... 例如: 工程是一个字符串包含了多少次“G “在完整的字符串检查字符串多少次包含字符串资格字符“G”
回答
使用正则表达式[g]
找到char和计算结果如下:
Pattern pattern = Pattern.compile("[g]");
Matcher matcher = pattern.matcher("engineering");
int countCharacter = 0;
while(matcher.find()) {
countCharacter++;
}
System.out.println(countCharacter);
如果你想不区分大小写计数,在模式中使用正则表达式作为[gG]
。
我会用一个Pattern
和Matcher
:
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++;
您可以尝试以下操作:
String str = "engineering";
int letterCount = 0;
int index = -1;
while((index = str.indexOf('g', index+1)) > 0)
letterCount++;
System.out.println("Letter Count = " + letterCount);
试试这个
int count = StringUtils.countMatches("engineering", "e");
更多StringUtils可以从这个问题可以了解到: How do I use StringUtils in Java?
你从哪里得到'StringUtils'? –
请检查commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html – sunleo
尽管正则表达式可以正常工作,但在这里并不需要。你可以简单地使用for-loop
来为一个角色维护一个count
。
您将需要您的字符串转换为字符数组: -
String str = "engineering";
char toCheck = 'g';
int count = 0;
for (char ch: str.toCharArray()) {
if (ch == toCheck) {
count++;
}
}
System.out.println(count);
或者,你也可以做到这一点无需转换到charArray
: -
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == toCheck) {
count++;
}
}
请检查是否有计数不匹配。 – sunleo
@sunleo ..对于哪些代码?那么,他们都工作得很好。刚刚检查过它。 –
@sunleo ..请尝试一下。在我的情况下,他们都给了'3'。 –
String s = "engineering";
char c = 'g';
s.replaceAll("[^"+ c +"]", "").length();
注意,如果'char c'来自用户输入,这种方法很容易受到正则表达式的注入。 (类似于SQL注入) – Tuupertunut
你可以通过它循环,并保持你想要的字母数。
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");
我知道这是老问题,但就是没有得到回答的选项,这是很简单的一行:
int count = string.length() - string.replaceAll("g","").length()
我认为你应该将beginIndex和endIndex添加到子字符串 –
这是一个非常非常古老的问题,但是这可能帮助别人( “_”)
您可以只需在使用此代码
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;
}
使用org.apache.c ommons.lang3包使用StringUtils类。 下载jar文件并将其放置到Web应用程序的lib文件夹中。
int count = StringUtils.countMatches("engineering", "e");
- 1. 检查字符串包含字符串
- 2. 检查字符串包含的字符
- 3. 检查字符串包含Unicode字符
- 4. 检查字符串多少次是一个字符串
- 5. 检查字符串是否包含字(不是子字符串!)
- 6. 检查字符串是否包含两次相同的字符
- 7. 检查字符串是否包含字符集中的字符
- 8. 如何检查是否字符串包含字符串数组字符串
- 9. 检查字符串是否包含空格和多个@符号
- 10. SELECT查询字符串包含'%'字符
- 11. Java:检查字符串是否包含多个字符
- 12. F#计算一个字符串包含多少次子字符串
- 13. React-Native:检查字符串是否包含字符串
- 14. Applescript:检查一个字符串是否包含空字符串?
- 15. 熊猫 - 检查字符串列包含一对字符串
- 16. 检查包含子字符串列表的字符串
- 17. 检查字符串是否不包含其他字符串
- 18. 包含在字符串列表中的检查字符串
- 19. 检查字符串是否包含子字符串
- 20. 检查字符串包含在字符串
- 21. 检查字符串元素包含数组字符串
- 22. PHP检查字符串是否包含数字和检查字符串长度
- 23. 检查字符串包含非数字字符
- 24. c# - 检查字符串是否包含字符和数字
- 25. 检查一个字符串不包含字母字符
- 26. 如何在Javascript中检查字符串包含字母字符
- 27. 检查字符串中是否包含字符和数字
- 28. 检测字符串只包含空格
- 29. 字符串包含
- 30. 字符串包含
将其转换为字符数组并循环检查匹配项。查看[java.util.String](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) – MadProgrammer