当前位置: 首页 > 面试题库 >

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

宣弘新
2023-03-14
问题内容

这是我的代码:

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");


 类似资料:
  • 除了前面介绍的几个方法外, Python 字符串变量还可以使用 startswith() 和endswith() 方法。 startswith()方法 startswith() 方法用于检索字符串是否以指定字符串开头,如果是返回 True;反之返回 False。此方法的语法格式如下: str.startswith(sub[,start[,end]]) 此格式中各个参数的具体含义如下: str:表示

  • 问题内容: 假设我有一个带有名为“ myfield”的字符串字段的类,并使用反射来获取该字段,我发现这是区分大小写的,如果我使用 有什么办法解决吗?强迫它忽略大小写? 谢谢 问题答案: 只需使用并查看结果即可自己执行不区分大小写的匹配。

  • 问题内容: 我该如何编写两个函数,这些函数将接受字符串并以指定的字符/字符串开头或以指定的字符串结尾? 例如: 问题答案: 如果您不想使用正则表达式,请使用此选项。

  • 问题内容: 我想使用方法但忽略大小写。 假设我有“会话”,并且在“ sEsSi”上使用了它,那么它应该返回。 我该如何实现? 问题答案: 在测试之前,请使用或标准化您的字符串。

  • 本文向大家介绍Python中的startswith和endswith函数使用实例,包括了Python中的startswith和endswith函数使用实例的使用技巧和注意事项,需要的朋友参考一下 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数判断文本是否以某个字符开始,endswith()函数判断文本是否以某个字

  • 问题内容: 我正在使用Java 7的新功能进行POC。我有在switch语句中使用String的代码,它可以工作。我也想使其不区分大小写。有没有一种方法可以在String上使用ignoreCase签出? 问题答案: 不,但是您可以打开。所以: 在我们挑剔的时候,您最好在英语语言环境中使用大写字母,以避免出现土耳其语问题