在Java中使用startsWith和endsWith时如何忽略大小写? [英] How do I ignore case when using startsWith and endsWith in Java?

查看:5193
本文介绍了在Java中使用startsWith和endsWith时如何忽略大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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 是否以任何一个结束左或右,虽然不区分大小写。这一切都可能吗?

I'd like to allow the user some leeway as to how they can enter the commands, things like: Top Right, top-right, TOPRIGHT, upper left, etc. To that end, I'm trying to, at that last if (, check if cutInput starts with either "top" or "up" AND check if cutInput ends with either "left" or "right", all while being case-insensitive. Is this at all possible?

最终目标是允许用户在一个输入线,从三角形的四个方向中选择一个。这是我能想到的最好的方式,但我仍然对编程很新,可能会使事情变得复杂。如果我是,和它变成了一种更简单的方式,请告诉我。

The end goal of this is to allow the user to, in one line of input, pick from one of four orientations of a triangle. This was the best way I could think of to do that, but I'm still quite new to programming in general and might be over complicating things. If I am, and it turns there's a simpler way, please let me know.

推荐答案

喜欢这个:

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

这篇关于在Java中使用startsWith和endsWith时如何忽略大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆