我正在尝试编写一些代码来告诉我一个字符串的第一个字母是否等于另一个字符串的第一个字母。我不知道如何将字符串与 charAt() 的返回值进行比较。帮忙?
public class CharAtTest
{
public static void main(String [] args)
{
String name = "joe";
String initial = "j";
if(initial.equals(name.charAt(0)))
{
System.out.println("Sucess");
}else{
System.out.println("Fail");
}
}
}
最佳答案
你可以使用:
if (initial.charAt(0) == name.charAt(0))
或更好
if (name.startsWith(initial)) {
关于java - charAt() 返回的不是字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13694689/