Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch ignore case in java 7

I am doing a POC on Java 7 new features. I have code to use String in switch statement and it works. I want to make it work in case insensitive also. Is there a way to check out with ignoreCase on String?

package com.java.j7;  public class Test {     final private String _NEW ="NEW";     final private String _PENDING = "PENDING";     final private String _CLOSED = "CLOSED";     final private String _REJECTED ="REJECTED";  public static void main(String... strings){      Test j = new Test();     j.processItem("new");     j.processItem("pending");     j.processItem("closed");     j.processItem("rejected");  }  void processItem(String s){     switch (s) {     case _NEW:         System.out.println("Matched to new");         break;     case _PENDING:         System.out.println("Matched to pending");         break;     case _CLOSED:         System.out.println("Matched to closed");         break;     case _REJECTED:         System.out.println("Matched to rejected");         break;      default:         System.out.println("Not matching any more");         break;     }  } } 
like image 413
Chowdappa Avatar asked Oct 28 '13 06:10

Chowdappa


People also ask

How do you make a switch statement ignore case?

To use case-insensitive switch-case in JavaScript, make the input all upper or all lowercase.

How do you ignore a case in java?

Java String equalsIgnoreCase() Method The 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 switch in java case sensitive?

Keys points to know for java switch case String are: Java switch case String make code more readable by removing the multiple if-else-if chained conditions. Java switch case String is case sensitive, the output of example confirms it.

Is case in switch case sensitive?

Case Sensitive Comparison: The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the equals() method of String class consequently, the comparison of String objects in switch statements is case sensitive.


2 Answers

no, but you could switch on s.toUpperCase(). so:

switch (s.toUpperCase()) {    //same as before } 

and while we're nitpicking, you better upper-case things in the english locale to avoid issues with turkish

like image 165
radai Avatar answered Sep 28 '22 00:09

radai


using String in switch Example from oracle docs Using Strings in switch Statements

 public class StringSwitchDemo {          public static int getMonthNumber(String month) {              int monthNumber = 0;              if (month == null) {                 return monthNumber;             }              switch (month.toLowerCase()) {                 case "january":                     monthNumber = 1;                     break;                 case "february":                     monthNumber = 2;                     break;                 case "march":                     monthNumber = 3;                     break;                 case "april":                     monthNumber = 4;                     break;                 case "may":                     monthNumber = 5;                     break;                 case "june":                     monthNumber = 6;                     break;                 case "july":                     monthNumber = 7;                     break;                 case "august":                     monthNumber = 8;                     break;                 case "september":                     monthNumber = 9;                     break;                 case "october":                     monthNumber = 10;                     break;                 case "november":                     monthNumber = 11;                     break;                 case "december":                     monthNumber = 12;                     break;                 default:                      monthNumber = 0;                     break;             }              return monthNumber;         }          public static void main(String[] args) {              String month = "August";              int returnedMonthNumber =                 StringSwitchDemo.getMonthNumber(month);              if (returnedMonthNumber == 0) {                 System.out.println("Invalid month");             } else {                 System.out.println(returnedMonthNumber);             }         }     } 
like image 22
Deepak Odedara Avatar answered Sep 28 '22 01:09

Deepak Odedara