在Java中使用startsWith和endsWith时,如何忽略大小写?
这是我的代码:
public static void rightSel(Scanner scanner,char t){
/*if (!stopping)*/System.out.print(": ");
if (scanner.hasNextLine())
{
String orInput = scanner.nextLine;
if (orInput.equalsIgnoreCase("help")
{
System.out.println("The following commands are available:");
System.out.println(" 'help' : displays this menu");
System.out.println(" 'stop' : stops the program");
System.out.println(" 'topleft' : makes right triangle alligned left and to the top");
System.out.println(" 'topright' : makes right triangle alligned right and to the top");
System.out.println(" 'botright' : makes right triangle alligned right and to the bottom");
System.out.println(" 'botleft' : makes right triangle alligned left and to the bottom");
System.out.println("To continue, enter one of the above commands.");
}//help menu
else if (orInput.equalsIgnoreCase("stop")
{
System.out.println("Stopping the program...");
stopping = true;
}//stop command
else
{
String rawInput = orInput;
String cutInput = rawInput.trim();
if (
对于用户如何输入命令,我希望允许他们留有余地,例如:右上,右上,TOPRIGHT,左上等。为此,我试图在最后一点if
(,检查是否cutInput
以“ top”或“ up”开头,并检查是否cutInput
以“ left”或“
right”结尾,并且不区分大小写。这是可能吗?
这样做的最终目的是允许用户在输入的一行中从三角形的四个方向之一中进行拾取。这是我想到的最好方法,但是对于一般的编程我还是很陌生,可能会使事情变得复杂。如果是的话,这实际上是一种更简单的方法,请告诉我。
回答:
像这样:
aString.toUpperCase().startsWith("SOMETHING");aString.toUpperCase().endsWith("SOMETHING");
以上是 在Java中使用startsWith和endsWith时,如何忽略大小写? 的全部内容, 来源链接: utcz.com/qa/409608.html