Java查找字符串中的特定字母。

huangapple go评论85阅读模式
英文:

Java Looking for specific letters in a sting

问题

以下是您的代码的翻译部分:

我在想如何迭代遍历一个字符串并检查有多少个 "hi" 出现

例如如果字符串是 "hihi"则计数应该输出 2

这是我目前的代码

public static int countHi(String str) {
    int counter = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str.substring(i, i + 1).equals("h")) {
            if (str.substring(i, i + 1).equals("i")) {
                counter = counter + 1;
            }
        }
    }
    return counter;
}

public static void main(String[] args) {
    String str = "hihi";
    int i = countHi(str);
    System.out.println("number of hi = " + i);
}
英文:

I was wondering how to iterate over a string and to check how many hi's come out

For example, if the string is "hihi" the count should output 2.

This is what I have so far

public static int countHi(String str) {
    int counter = 0;
    for (int i = 0; i &lt; str.length(); i++) {
        if (str.substring(i, i + 1) == &quot;h&quot;) {
            if (str.substring(i, i + 1) == &quot;i&quot;) {
                counter = counter + 1;
            }
        }
    }
    return counter;
}

public static void main(String[] args) {
    String str = &quot;hihi&quot;;
    int i = countHi(str);
    System.out.println(&quot;number of hi = &quot; + i);
}

答案1

得分: 1

你应该使用.equals来比较实例(例如 String),而不是使用==。然而,在这里,你可以在String.charAt(int)中使用==。另外,我建议从第二个字符开始,将当前索引处的字符与i以及前一个索引处的字符与h进行比较。像这样:

public static int countHi(String str) {
    int counter = 0;
    for (int i = 1; i < str.length(); i++) {
        if (str.charAt(i - 1) == 'h' && str.charAt(i) == 'i') {
            counter++;
        }
    }
    return counter;
}

或者,你可以将当前索引处的字符与h进行比较,将下一个索引处的字符与i进行比较(但现在需要提前停止迭代一个字符)。像这样:

public static int countHi(String str) {
    int counter = 0;
    for (int i = 0; i < str.length() - 1; i++) {
        if (str.charAt(i) == 'h' && str.charAt(i + 1) == 'i') {
            counter++;
        }
    }
    return counter;
}
英文:

You compare instances (like String) with .equals (not ==). However, here you can use == with String.charAt(int). Also, I would start with the second character and compare the character at the current index with i and the previous index with h. Like,

public static int countHi(String str) {
	int counter = 0;
	for (int i = 1; i &lt; str.length(); i++) {
		if (str.charAt(i - 1) == &#39;h&#39; &amp;&amp; str.charAt(i) == &#39;i&#39;) {
			counter++;
		}
	}
	return counter;
}

Alternatively, compare the character at the current index with h and the character at the next index with i (but now you need to stop iterating a character earlier). Like,

public static int countHi(String str) {
	int counter = 0;
	for (int i = 0; i &lt; str.length() - 1; i++) {
		if (str.charAt(i) == &#39;h&#39; &amp;&amp; str.charAt(i + 1) == &#39;i&#39;) {
			counter++;
		}
	}
	return counter;
}

答案2

得分: 0

这是简单的方法:

public static int countHi(String str) {
    return split(str, -1).length - 1;
}

请注意,您必须将split()的第二个参数传递为-1;如果没有这个参数,结果中的尾随空格将被删除。

英文:

Here's the easy way:

public static int countHi(String str) {
    return split(str, -1).length - 1;
}

Note that you must pass -1 as the second parameter of split(); without it, trailing blanks would be pruned from the result.

答案3

得分: 0

为了紧凑和可读性,也许可以:

int count = 0;
Matcher matcher = Pattern.compile("hi").matcher(string)
while (matcher.find()) {
    count++;
}

这种方法适用于任何正则表达式模式,尽管不是最高效的。

英文:

For compactness and readability, maybe:

int count = 0;
Matcher matcher = Pattern.compile(“hi”).matcher(string)
while (matcher.find()) {
    count++;
}

This approach will work for any Regular Expression pattern, although it won’t be the most efficient.

huangapple
  • 本文由 发表于 2020年8月7日 07:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63293075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定