Java 检查一个字母是否在字符串(单词)中,它在什么位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9846737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if one letter is in a string(word), and what position it is at?
提问by sindrem
I have one method with one letter(string\char?) as argument and one word active at a time. I would like to see if this one letter, is in the word which is active at the time.
我有一种方法,用一个字母(字符串\字符?)作为参数,一次激活一个单词。我想看看这封信是否在当时活跃的词中。
That could probably have been explained better. Maybe code will do the trick:
这可能可以更好地解释。也许代码可以解决问题:
public void checkLetter(String letter){
for(int i = 0; i<activeWord.length(); i++){
if(letter.equals(activeWord.[i])){
// Run a method which gives the user a correct letter pushed.
}
else {
String failed = "Could not find the letter in the active word.";
// Run a method which gives the user one wrong letter pushed.
}
}
}
So, this is going to be a hangman game for android i'm making for fun. Right now i have images as letters. When the letter(image) is pressed, this method runs, and the letter pushed will be the argument.
所以,这将是一个我为了好玩而制作的 android 刽子手游戏。现在我有图像作为字母。当按下字母(图像)时,此方法运行,并且按下的字母将作为参数。
Does anyone have any good ideas how i can find out if the letter is in the word, and which posistion it is in?
有没有人有什么好主意,我如何才能找出字母是否在单词中,以及它在哪个位置?
I would love some help :)
我希望得到一些帮助:)
EDIT: Btw, the current code returns int every time ofc, cause the [i] is an int. This is where im strugling to get the code to return a string. Do i have to split the word up into letter before i can equal them to another string? Thanks.
编辑:顺便说一句,当前代码每次 ofc 都返回 int,因为 [i] 是一个 int。这是我努力让代码返回一个字符串的地方。在我可以将它们等于另一个字符串之前,我是否必须将单词拆分为字母?谢谢。
采纳答案by twain249
use String's methods
使用字符串的方法
int i = 0;
while(word.indexOf(i, letter) != -1) {
i = word.indexOf(i, letter) + 1;
//Do whatever
}
if(i == 0) {
//Handle missed letter
}
回答by Jon
You are looking for String.indexOf
:
您正在寻找String.indexOf
:
int pos = activeWord.indexOf(letter);
if (pos == -1) {
// letter not found
}
else {
// letter exists at zero-based position pos
// CAUTION: it might also exist in later positions!
}
回答by Alex
use String.indexOf(String s)
, which returns the index of the first occurrence, or -1 if not found
use String.indexOf(String s)
,它返回第一次出现的索引,如果没有找到,则返回 -1
回答by Griffin
Use String.indexOf()
用 String.indexOf()
String myString = "word";
myString.indexOf("w"); // this returns 0;
myString.indexOf("h"); // this returns -1
This will only give you the position of first appearance of the letter in the string. To get other positions you will need to take the substring from the last position and repeat the process.
这只会给你字母在字符串中第一次出现的位置。要获得其他位置,您需要从最后一个位置获取子字符串并重复该过程。
回答by Adam
How about a daily-WTF inspired bruteforce regex solution? Otherwise just use String.indexOf(char)...
每日 WTF 启发的蛮力正则表达式解决方案怎么样?否则只需使用 String.indexOf(char) ...
String input = "Hello world";
char test = 'o';
int found = -1;
for (int i = 0; i < input.length(); i++) {
StringBuilder builder = new StringBuilder();
for (int c = 0; c < input.length(); c++) {
if (c == i) {
builder.append(test);
} else {
builder.append(".");
}
}
Pattern p = Pattern.compile(builder.toString());
if (p.matcher(input).find()) {
found = i;
break;
}
}
System.out.println(found);