3.1*_*per -1 java string boolean
我一直在搜索stackoverflow,我发现了一些关于将字符串转换为布尔值的其他问题,但我无法使其工作.也许这只是我尝试使用它的方式是不正确的.
无论如何,我试图将两个不同的输入字符串"M"或"I"转换为布尔值,以便在if语句中使用.什么基本上是希望功能是这样的:
// the text that is retrieved is assumed to be either"M" or "I"
M=Input.getText
I=Input.getText
If M shows the value "M",
do stuff here
else if I shows the value "I",
do stuff here
else if neither above are true,
throw an exception here
Run Code Online (Sandbox Code Playgroud)
我已经尝试了任何数量的"toBoolean"和"Boolean.valueof",但我尝试的都没有.
PS,很抱歉没有真正的代码可以使用,这是我的第一步,因此我没有围绕这篇文章构建任何东西.
您可以使用String方法来检查它是否包含给定的文字值,等于它,或等于忽略大小写.
草案条件是:
if ("myValue".equalsIgnoreCase(myText)) {
// TODO
}
else if ("myOtherValue".equalsIgnoreCase(myOtherText)) {
// TODO
}
else {
// TODO
}
Run Code Online (Sandbox Code Playgroud)
以下是以下文档java.lang.String:
你还需要检查的许多其他方法,比如startsWith,endswith等等等等.