Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ignore case when using startsWith and endsWith in Java? [duplicate]

Here's my code:

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 ( 

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.

like image 422
Matao Gearsky Avatar asked Nov 07 '14 04:11

Matao Gearsky


People also ask

How do you disregard a case in Java?

Java String equalsIgnoreCase() MethodThe equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not. Tip: Use the compareToIgnoreCase() method to compare two strings lexicographically, ignoring case differences.

Is startsWith () case sensitive?

startsWith method is case sensitive. However there is a String. startsWithIgnoreCase method which as the name notes is case insensitive.

Does Java string contain case insensitive?

Yes, contains is case sensitive. You can use java. util.

What is the return type of startsWith ()?

Returns: A boolean value: true - if the string starts with the specified character(s) false - if the string does not start with the specified character(s)


1 Answers

Like this:

aString.toUpperCase().startsWith("SOMETHING"); aString.toUpperCase().endsWith("SOMETHING"); 
like image 81
Óscar López Avatar answered Sep 19 '22 19:09

Óscar López