您如何检查字符串中的第一个字母是否等于某物? [英] How do you check to see if the 1st letter in a string is equal to something?

查看:49
本文介绍了您如何检查字符串中的第一个字母是否等于某物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的if语句中,我试图检查 string 的首字母是 Y 还是 y ,然后照这样进行.以下是我所拥有的,但我认为这是不正确的.

In my if statement, I'm trying to check if the first letter of a string is either Y or y, and then proceed as such. Below is what I have, but I don't believe it to be correct.

System.out.print("Do you wish to do another calculation (Yes/No): ")
      option = scan.next();
         if (option.substring(0,1) == "N" && option.substring(0,1) == "n" )
         {
            System.out.println("Have a good day");
            System.exit(0);
         }
bmi.setOption(option);

我早先在程序中将 option 实例化为 String option ="; .我知道如何检查字符串是否等于某个字符,但是很难检查字符串的第一个字符是否等于某个字符.

I instantiated option as String option = " "; earlier on in my program. I know how to check if strings equal a certain character, however am having trouble checking to see if only the FIRST character of the string is equal to something.

谢谢!

推荐答案

if (Character.toLowerCase(option.charAt(0)) == 'n')

这篇关于您如何检查字符串中的第一个字母是否等于某物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆