a.b.c.d
我希望以惯用方式计算“.”的出现次数,最好是一行代码解决。(之前我曾表达过这个限制条件为“不使用循环”,以防你想知道为什么每个人都试图回答而不使用循环)。
a.b.c.d
我希望以惯用方式计算“.”的出现次数,最好是一行代码解决。受Yonatan解决方案的启发,这里有一个完全基于递归的解决方案 - 仅使用了length()
和charAt()
两种库方法,它们都没有进行任何循环:
public static int countOccurrences(String haystack, char needle)
{
return countOccurrences(haystack, needle, 0);
}
private static int countOccurrences(String haystack, char needle, int index)
{
if (index >= haystack.length())
{
return 0;
}
int contribution = haystack.charAt(index) == needle ? 1 : 0;
return contribution + countOccurrences(haystack, needle, index+1);
}
递归是否算作循环取决于你使用的确切定义,但这可能是你能得到的最接近循环的东西。
我不知道大多数JVM现在是否支持尾递归... 如果不支持,则对于足够长的字符串,您将遇到同名的堆栈溢出问题。
我不喜欢为此目的分配新字符串的想法。由于字符串已经在后端有一个存储其值的字符数组,所以String.charAt()实际上是免费的。
for(int i=0;i<s.length();num+=(s.charAt(i++)==delim?1:0))
在不需要进行额外内存分配和垃圾回收的情况下,只需使用 J2SE 中的 1 行或更少代码即可完成操作。
charAt
迭代 16 位代码点而不是字符!在 Java 中,char
不是一个字符。因此,这个答案意味着没有 Unicode 符号的高代理等于 delim
的代码点。我不确定对于点是否正确,但一般来说可能不正确。 - cevingpublic 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;
}
我不确定这个方法的效率如何,但这是我在不使用第三方库的情况下能够编写的最短代码:
public static int numberOf(String target, String content)
{
return (content.split(target).length - 1);
}
return (content.split(target, -1).length - 1);
。默认情况下,在split()方法返回的数组中,末尾处的出现次数会被省略掉。请参考Doku。 - vlz使用java-8,你也可以使用流来实现这一点。显然,在幕后有一个迭代,但你不必明确地编写它!
public static long countOccurences(String s, char c){
return s.chars().filter(ch -> ch == c).count();
}
countOccurences("a.b.c.d", '.'); //3
countOccurences("hello world", 'l'); //3
.codePoints()
而不是.chars()
将支持任何Unicode值(包括那些需要代理对的值)。 - Luke Usherwood在Java 8中,也可以使用reduce来解决这个问题:
int res = "abdsd3$asda$asasdd$sadas".chars().reduce(0, (a, c) -> a + (c == '$' ? 1 : 0));
System.out.println(res);
输出:
3
只需按照以下步骤操作:
public static void main(String[] args) {
String string = "a.b.c.d";
String []splitArray = string.split("\\.",-1);
System.out.println("No of . chars is : " + (splitArray.length-1));
}
完整示例:
public class CharacterCounter
{
public static int countOccurrences(String find, String string)
{
int count = 0;
int indexOf = 0;
while (indexOf > -1)
{
indexOf = string.indexOf(find, indexOf + 1);
if (indexOf > -1)
count++;
}
return count;
}
}
电话:
int occurrences = CharacterCounter.countOccurrences("l", "Hello World.");
System.out.println(occurrences); // 3
如果您正在使用Spring框架,您也可以使用“StringUtils”类。方法将是“countOccurrencesOf”。
一种更简单的解决方案是根据匹配字符来拆分字符串。
例如,
int getOccurences(String characters, String string) {
String[] words = string.split(characters);
return words.length - 1;
}
在以下情况下将返回4:
getOccurences("o", "something about a quick brown fox");