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

Mat*_*sky 27 java string startswith case-insensitive 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 (
Run Code Online (Sandbox Code Playgroud)

我想让用户有一些余地来了解他们如何输入命令,例如:右上角,右上角,右上角,左上角等等.为此,我试图在最后if (,检查是否cutInput以"top"或"up"开头并检查是否cutInput以"left"或"right"结束,所有这些都是不区分大小写的.这是可能吗?

这样做的最终目的是允许用户在一行输入中从三角形的四个方向中选择一个.这是我能想到的最好的方式,但是对于一般的编程我来说还是很新的,可能会使事情变得复杂.如果我是,并且它转向更简单的方式,请告诉我.

Ósc*_*pez 47

像这样:

aString.toUpperCase().startsWith("SOMETHING");
aString.toUpperCase().endsWith("SOMETHING");
Run Code Online (Sandbox Code Playgroud)

  • 这个答案是对的.如果你看一下`String.equalsIgnoreCase()`的实现,你会发现你需要比较字符串的小写和大写版本才能最终返回'false`.请参阅http://stackoverflow.com/a/38947571/14731上的答案. (3认同)

Gil*_*ili 22

接受的答案是错误的.如果你看的实施String.equalsIgnoreCase(),你会发现,你需要比较两个字符串的小写和大写的版本,你可以决定性地回来之前false.

这是我自己的版本,基于http://www.java2s.com/Code/Java/Data-Type/CaseinsensitivecheckifaStringstartswithaspecifiedprefix.htm:

/**
 * String helper functions.
 *
 * @author Gili Tzabari
 */
public final class Strings
{
    /**
     * @param str    a String
     * @param prefix a prefix
     * @return true if {@code start} starts with {@code prefix}, disregarding case sensitivity
     */
    public static boolean startsWithIgnoreCase(String str, String prefix)
    {
        return str.regionMatches(true, 0, prefix, 0, prefix.length());
    }

    public static boolean endsWithIgnoreCase(String str, String suffix)
    {
        int suffixLength = suffix.length();
        return str.regionMatches(true, str.length() - suffixLength, suffix, 0, suffixLength);
    }

    /**
     * Prevent construction.
     */
    private Strings()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @AleksanderLech 我使用 Java 编程已有 20 多年了。与制表符与空格的争论类似,不同的人/公司使用他们认为合适的任何格式。我的特定标准涉及在打开大括号之前换行。我这样做是因为我觉得生成的代码更具可读性(开/闭括号垂直排列)。每个人都有自己的。 (3认同)
  • 旁注:Spring Framework 也与 Gili 的回答一致:https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/util/StringUtils。 java#L330 (2认同)