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

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

Java Looking for specific letters in a sting

问题

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

  1. 我在想如何迭代遍历一个字符串并检查有多少个 "hi" 出现
  2. 例如如果字符串是 "hihi"则计数应该输出 2
  3. 这是我目前的代码
  4. public static int countHi(String str) {
  5. int counter = 0;
  6. for (int i = 0; i < str.length(); i++) {
  7. if (str.substring(i, i + 1).equals("h")) {
  8. if (str.substring(i, i + 1).equals("i")) {
  9. counter = counter + 1;
  10. }
  11. }
  12. }
  13. return counter;
  14. }
  15. public static void main(String[] args) {
  16. String str = "hihi";
  17. int i = countHi(str);
  18. System.out.println("number of hi = " + i);
  19. }
英文:

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

  1. public static int countHi(String str) {
  2. int counter = 0;
  3. for (int i = 0; i &lt; str.length(); i++) {
  4. if (str.substring(i, i + 1) == &quot;h&quot;) {
  5. if (str.substring(i, i + 1) == &quot;i&quot;) {
  6. counter = counter + 1;
  7. }
  8. }
  9. }
  10. return counter;
  11. }
  12. public static void main(String[] args) {
  13. String str = &quot;hihi&quot;;
  14. int i = countHi(str);
  15. System.out.println(&quot;number of hi = &quot; + i);
  16. }

答案1

得分: 1

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

  1. public static int countHi(String str) {
  2. int counter = 0;
  3. for (int i = 1; i < str.length(); i++) {
  4. if (str.charAt(i - 1) == 'h' && str.charAt(i) == 'i') {
  5. counter++;
  6. }
  7. }
  8. return counter;
  9. }

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

  1. public static int countHi(String str) {
  2. int counter = 0;
  3. for (int i = 0; i < str.length() - 1; i++) {
  4. if (str.charAt(i) == 'h' && str.charAt(i + 1) == 'i') {
  5. counter++;
  6. }
  7. }
  8. return counter;
  9. }
英文:

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,

  1. public static int countHi(String str) {
  2. int counter = 0;
  3. for (int i = 1; i &lt; str.length(); i++) {
  4. if (str.charAt(i - 1) == &#39;h&#39; &amp;&amp; str.charAt(i) == &#39;i&#39;) {
  5. counter++;
  6. }
  7. }
  8. return counter;
  9. }

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,

  1. public static int countHi(String str) {
  2. int counter = 0;
  3. for (int i = 0; i &lt; str.length() - 1; i++) {
  4. if (str.charAt(i) == &#39;h&#39; &amp;&amp; str.charAt(i + 1) == &#39;i&#39;) {
  5. counter++;
  6. }
  7. }
  8. return counter;
  9. }

答案2

得分: 0

这是简单的方法:

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

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

英文:

Here's the easy way:

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

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

答案3

得分: 0

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

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

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

英文:

For compactness and readability, maybe:

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

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:

确定