How to ignore case when using startWith and ends using Java?

Here is 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 would like the user to make it clear how they can enter commands, for example: "Top Right", "Top Right", "TOPRIGHT", Top Left, etc. To this end, I try in the last if ( check if cutInput starts with "top" or "up" and checks if cutInput ends with "left" or "right", all being case insensitive. Is this possible?

The ultimate goal is to allow the user in one line of input to select one of the four orientations of the triangle. It was the best way to do this, but I'm still pretty new to programming in general and maybe complicating things too much. If I, and it turns there is an easier way, please let me know.

+16
java string case-insensitive startswith ends-with
Nov 07 '14 at 4:51 on
source share
3 answers

Like this:

 aString.toUpperCase().startsWith("SOMETHING"); aString.toUpperCase().endsWith("SOMETHING"); 
+30
Nov 07 '14 at 4:55
source share

The accepted answer is incorrect. If you look at the implementation of String.equalsIgnoreCase() , you will find that you need to compare both the string and string versions of the strings before you can finally return false .

Here is my own version based on 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() { } } 
+10
Aug 14 '16 at 23:34
source share

I did an exercise in my book, and the exercise said: “Make a method that checks to see if the end of the string ends with“ ger. ”Write a code where it checks any combination of uppercase and lowercase letters in the phrase“ ger ”.

So basically, he asked me to check the phrase on the line and ignore the case, so it doesn’t matter if the letters in "ger" are upper or lower case. Here is my solution:

 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; } 

}

as you can see from the code, I just converted the string that the user will enter in all lowercase letters. It would not matter if each letter alternated between lower and upper case, because I denied it.

0
Sep 06 '17 at 23:34 on
source share



All Articles