int count = "a.b.c.d".length() - "a.b.c.d".replace(".", "").length();
不知道为什么接受使用StringUtils的解决方案 .
2
String s = "a.b.c.d";
long result = s.chars().filter(ch -> ch == '.').count();
2
一个较短的例子是
String text = "a.b.c.d";
int count = text.split("\\.",-1).length-1;
17
这是一个没有循环的解决方案:
public static int countOccurrences(String haystack, char needle, int i){
return ((i=haystack.indexOf(needle, i)) == -1)?0:1+countOccurrences(haystack, needle, i+1);}
System.out.println("num of dots is "+countOccurrences("a.b.c.d",'.',0));
public static int countOccurrences(CharSequeunce haystack, char needle) {
return countOccurrences(haystack, needle, 0, haystack.length);
}
// Alternatively String.substring/subsequence use to be relatively efficient
// on most Java library implementations, but isn't any more [2013].
private static int countOccurrences(
CharSequence haystack, char needle, int start, int end
) {
if (start == end) {
return 0;
} else if (start+1 == end) {
return haystack.charAt(start) == needle ? 1 : 0;
} else {
int mid = (end+start)>>>1; // Watch for integer overflow...
return
countOccurrences(haystack, needle, start, mid) +
countOccurrences(haystack, needle, mid, end);
}
}
(免责声明:未经测试,未编译,不合理 . )
也许最好的(单线程,没有代理对支持)方式来编写它:
public static int countOccurrences(String haystack, char needle) {
int count = 0;
for (char c : haystack.toCharArray()) {
if (c == needle) {
++count;
}
}
return count;
}
26
不确定这个效率,但它是我可以编写的最短代码而不引入第三方库:
public static int numberOf(String target, String content)
{
return (content.split(target).length - 1);
}
public static void main(String[] args) {
String string = "a.b.c.d";
String []splitArray = string.split("\\.");
System.out.println("No of . chars is : " + splitArray.length-1);
}
212
也可以在Java 8中使用reduce来解决这个问题:
int res = "abdsd3$asda$asasdd$sadas".chars().reduce(0, (a, c) -> a + (c == '$' ? 1 : 0));
System.out.println(res);
输出:
3
7
import java.util.Scanner;
class apples {
public static void main(String args[]) {
Scanner bucky = new Scanner(System.in);
String hello = bucky.nextLine();
int charCount = hello.length() - hello.replaceAll("e", "").length();
System.out.println(charCount);
}
}// COUNTS NUMBER OF "e" CHAR´s within any string input
public static int count( final String s, final char c ) {
final char[] chars = s.toCharArray();
int count = 0;
for(int i=0; i<chars.length; i++) {
if (chars[i] == c) {
count++;
}
}
return count;
}
int numDots = 0;
if (s.charAt(0) == '.') {
numDots++;
}
if (s.charAt(1) == '.') {
numDots++;
}
if (s.charAt(2) == '.') {
numDots++;
}
...等等,但是你是在源编辑器中手动执行循环的人 - 而不是运行它的计算机 . 看到伪代码:
create a project
position = 0
while (not end of string) {
write check for character at position "position" (see above)
}
write code to output variable "numDots"
compile program
hand in homework
do not think of the loop that your "if"s may have been optimized and compiled to
5
这是一个略有不同的样式递归解决方案:
public static int countOccurrences(String haystack, char needle)
{
return countOccurrences(haystack, needle, 0);
}
private static int countOccurrences(String haystack, char needle, int accumulator)
{
if (haystack.length() == 0) return accumulator;
return countOccurrences(haystack.substring(1), needle, haystack.charAt(0) == needle ? accumulator + 1 : accumulator);
}
21
为什么不拆分字符然后获得结果数组的长度 . 数组长度总是实例数1.对吧?
3
以下源代码将为您提供用户输入的单词中给定字符串的出现次数: -
import java.util.Scanner;
public class CountingOccurences {
public static void main(String[] args) {
Scanner inp= new Scanner(System.in);
String str;
char ch;
int count=0;
System.out.println("Enter the string:");
str=inp.nextLine();
while(str.length()>0)
{
ch=str.charAt(0);
int i=0;
while(str.charAt(i)==ch)
{
count =count+i;
i++;
}
str.substring(count);
System.out.println(ch);
System.out.println(count);
}
}
}
2
int count = (line.length() - line.replace("str", "").length())/"str".length();
30 回答
我的'惯用单线'是这样的:
当它已经在commons lang时,为什么要自己写呢?
Spring Framework的oneliner就是:
这个怎么样 . 它不使用下面的regexp,所以应该比其他一些解决方案更快,并且不会使用循环 .
总结其他答案以及我所知道的使用单线程的所有方法:
1)使用 Apache Commons
2)使用 Spring Framework's
3)使用 replace
4)使用 replaceAll (案例1)
5)使用 replaceAll (案例2)
6)使用 split
7)使用 Java8 (案例1)
8)使用 Java8 (情况2),对于unicode可能比情况1更好
9)使用 StringTokenizer
From comment :小心使用StringTokenizer,对于abcd它可以工作但是对于...... bc ... d或... abcd或者...... b ...... c ..... d . ..等等它不会起作用 . 它只是值得 . 人物之间只有一次
更多信息github
Perfomance test(使用JMH,模式= AverageTime,得分
0.010
优于0.351
):迟早,某些东西必须循环 . 编写(非常简单)循环比使用类似
split
的东西要简单得多,这比你需要的强大得多 .通过所有方法将环路封装在单独的方法中,例如,
然后你不需要在主代码中使用循环 - 但循环必须在某处 .
我有一个类似于Mladen的想法,但相反......
ReplaceAll(“ . ”)将替换所有字符 .
PhiLho's solution使用ReplaceAll("[^.]",“”),它不需要转义,因为[ . ]代表字符'dot',而不是'any character' .
我的'惯用单线'解决方案:
不知道为什么接受使用StringUtils的解决方案 .
一个较短的例子是
这是一个没有循环的解决方案:
好吧,有一个循环,但它是 invisible :-)
我不喜欢为此目的分配新字符串的想法 . 由于字符串后面已经有一个char数组,它存储了它的值,String.charAt()实际上是免费的 .
如果没有需要收集的额外分配,只需要一行或更少,只需要J2SE .
好吧,受到Yonatan 's solution, here'的启发,它纯粹是递归的 - 唯一使用的库方法是
length()
和charAt()
,它们都没有做任何循环:递归计数是否为循环取决于您使用的确切定义,但它可能与您获得的接近 .
我不知道这些天大多数JVM是否会进行尾递归...如果不是,你会得到适当长串的同名堆栈溢出,当然 .
灵感来自Jon Skeet,一个不会让你的筹码无法破坏的非循环版本 . 如果要使用fork-join框架,也是有用的起点 .
(免责声明:未经测试,未编译,不合理 . )
也许最好的(单线程,没有代理对支持)方式来编写它:
不确定这个效率,但它是我可以编写的最短代码而不引入第三方库:
使用java-8,您还可以使用流来实现此目的 . 显然幕后有一个迭代,但你不必明确地写它!
Complete sample:
Call:
如果您使用的是Spring框架,也可以使用“StringUtils”类 . 该方法将是“countOccurrencesOf” .
获得答案的最简单方法如下:
也可以在Java 8中使用reduce来解决这个问题:
输出:
您只需一行代码即可使用
split()
功能虽然方法可以隐藏它,但没有循环(或递归)的计数是没有办法的 . 出于性能原因,您希望使用char [] .
使用replaceAll(即RE)听起来不是最好的方式 .
在代码中的某个地方,必须循环 . 解决这个问题的唯一方法是完全展开循环:
...等等,但是你是在源编辑器中手动执行循环的人 - 而不是运行它的计算机 . 看到伪代码:
这是一个略有不同的样式递归解决方案:
为什么不拆分字符然后获得结果数组的长度 . 数组长度总是实例数1.对吧?
以下源代码将为您提供用户输入的单词中给定字符串的出现次数: -
使用Eclipse Collections
如果要计算多个字符,可以使用
CharBag
,如下所示:注意:我是Eclipse Collections的提交者 .
好吧,有一个非常相似的任务我偶然发现了这个线程 . 我没有看到任何编程语言限制,因为groovy在java vm上运行:这是我如何使用Groovy解决我的问题 .
完成 .