46

这是我的代码:

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”结尾,同时不区分大小写。这是可能吗?

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

4

3 回答 3

65

像这样:

aString.toUpperCase().startsWith("SOMETHING");
aString.toUpperCase().endsWith("SOMETHING");
于 2014-11-07T04:55:14.660 回答
41

接受的答案是错误的。如果您查看实现,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()
    {
    }
}
于 2016-08-14T23:34:54.360 回答
0

我在我的书中做一个练习,练习说:“制作一个方法来测试字符串的结尾是否以 '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;
}

}

正如您从代码中看到的那样,我只是将用户输入的字符串转换为全部小写。如果每个字母都在小写和大写之间交替出现也没关系,因为我否定了这一点。

于 2017-09-06T23:34:46.717 回答