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

时间:2014-11-07 04:51:17

标签: 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 (

我想让用户了解如何输入命令,例如:右上角,右上角,右上角,左上角等。 为此,我正在尝试,在最后if (,检查cutInput是以“top”还是“up”开头并检查cutInput是否以“left”结尾或者“正确”,同时不区分大小写。这有可能吗?

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

3 个答案:

答案 0 :(得分:44)

像这样:

aString.toUpperCase().startsWith("SOMETHING");
aString.toUpperCase().endsWith("SOMETHING");

答案 1 :(得分:20)

接受的答案是错误的。如果您查看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()
    {
    }
}

答案 2 :(得分:1)

我正在书中练习,练习说,"制作一种方法,测试一下弦的结尾是否以“ger”结尾。'将代码写入测试的地方,以便在短语“ger”中使用大写和小写字母的任意组合。''"

所以,基本上,它要求我在一个字符串中测试一个短语并忽略这个案例,所以如果" ger"中的字母不重要。是大写还是小写。这是我的解决方案:

package exercises;

import javax.swing.JOptionPane;

public class exercises
{
    public static void main(String[] args)
    {
        String input, message = "enter a string. It will"
                                + " be tested to see if it "
                                + "ends with 'ger' at the end.";



    input = JOptionPane.showInputDialog(message);

    boolean yesNo = ends(input);

    if(yesNo)
        JOptionPane.showMessageDialog(null, "yes, \"ger\" is there");
    else
        JOptionPane.showMessageDialog(null, "\"ger\" is not there");
}

public static boolean ends(String str)
{
    String input = str.toLowerCase();

    if(input.endsWith("ger"))
        return true;
    else 
        return false;
}

}

从代码中可以看出,我只是将用户输入的字符串转换为所有小写字母。如果每个字母在大写和大写之间交替都没关系,因为我否定了这一点。