英文:
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 < str.length(); i++) {
if (str.substring(i, i + 1) == "h") {
if (str.substring(i, i + 1) == "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);
}
答案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 < str.length(); i++) {
if (str.charAt(i - 1) == 'h' && str.charAt(i) == 'i') {
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 < str.length() - 1; i++) {
if (str.charAt(i) == 'h' && str.charAt(i + 1) == 'i') {
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论