在java中的if语句中使用.indexOf()

在java中的if语句中使用.indexOf(),java,Java,我需要使用indexof方法,但terminal告诉我它需要一个变量,但它得到一个值。你们能解释一下我是怎么解决的吗 这是我自己想出来的 char a = 'a'; if (s.indexOf(a, s.length()) == 61) System.out.println(" Your string contains the letter 'a' at index position: " + s.indexOf(97)); else System

我需要使用indexof方法,但terminal告诉我它需要一个变量,但它得到一个值。你们能解释一下我是怎么解决的吗

这是我自己想出来的

char a = 'a';
    if (s.indexOf(a, s.length()) == 61)
        System.out.println(" Your string contains the letter 'a' at index position: " + s.indexOf(97));
    else
        System.out.println(" Your string does not contain the letter 'a'");

如果您使用的是Java。方法返回string
s
中的第一个位置,该位置包含在方法中传递的字符串。如果找不到字符串,则返回-1

if (s.indexOf("a") >= 0)
        System.out.println(" Your string contains the letter 'a' at index position: " + s.indexOf("a"));
    else
        System.out.println(" Your string does not contain the letter 'a'");

你能指定你正在使用的语言吗?你添加的标签都是不相关的。在我看来像Java。非常感谢这项工作,但我不明白为什么。你能给我解释一下吗?@WeekzSO如果成功了,你应该接受答案。它也将帮助其他可能面临同样问题的人。@WeekzSO我添加了一个指向java API的链接,并做了一个简短的解释:)@GPRathour我再过6分钟就不能接受这个答案了。