java - 在Java中使用startsWith和endsWith时如何忽略大小写?

标签 java string case-insensitive startswith ends-with

这是我的代码:

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 - 在Java中使用startsWith和endsWith时如何忽略大小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794275/

相关文章:

java - 使 JOptionPane 不区分大小写?

postgresql - 如何在 Postgresql 中进行 "case-insensitive"查询?

java - 用java语句执行字符串

java - GSON 解析 - 不同类型的相同 key

java - 如何比较基本类型中忽略大小写的字符

PHP - 使用字符串的 stripshales 后出现斜线

c# - 提取字符串中两个字符之间的子字符串 C#

java - 带代理的 SseEmitter

java - 如何使类可以从不同的包访问,但不能从java中的不同库/jar访问?

python - 如何使用正则表达式/Python 查找已知字符串、未知字符串和另一个已知字符串之后的所有整数?