簡體   English   中英

java string.contains in switch 語句

[英]java string.contains in switch statement

如何將以下代碼轉換為 switch 語句?

String x = "user input";

if (x.contains("A")) {
    //condition A;
} else if (x.contains("B")) {
    //condition B;
} else if(x.contains("C")) {
    //condition C;
} else {
    //condition D;
}

有一種方法,但不使用contains 你需要一個正則表達式。

final Matcher m = Pattern.compile("[ABCD]").matcher("aoeuaAaoe");
if (m.find())
  switch (m.group().charAt(0)) {
  case 'A': break;
  case 'B': break;
  }

你不能switchx.contains()這樣的條件。 Java 7 支持switch字符串,但不是你想要的。 使用if等。

java中的switch語句中不允許條件匹配。

您可以在這里做的是創建一個字符串文字的枚舉,並使用該枚舉創建一個輔助函數,該函數返回匹配的枚舉文字。 使用返回的枚舉值,您可以輕松應用switch case

例如:

public enum Tags{
   A("a"),
   B("b"),
   C("c"),
   D("d");

   private String tag;

   private Tags(String tag)
   {
      this.tag=tag;
   }

   public String getTag(){
      return this.tag;
   }

   public static Tags ifContains(String line){
       for(Tags enumValue:values()){
         if(line.contains(enumValue)){
           return enumValue;
         }
       }
       return null;
   }

}

在您的 Java 匹配類中,執行以下操作:

Tags matchedValue=Tags.ifContains("A");
if(matchedValue!=null){
    switch(matchedValue){
       case A:
         break;
       etc...
}
@Test
public void test_try() {
    String x = "userInputA"; // -- test for condition A
    String[] keys = {"A", "B", "C", "D"};
    String[] values = {"conditionA", "conditionB", "conditionC", "conditionD"};

    String match = "default";
    for (int i = 0; i < keys.length; i++) {
        if (x.contains(keys[i])) {
            match = values[i];
            break;
        }
    }

    switch (match) {
        case "conditionA":
            System.out.println("some code for A");
            break;
        case "conditionB":
            System.out.println("some code for B");
            break;
        case "conditionC":
            System.out.println("some code for C");
            break;
        case "conditionD":
            System.out.println("some code for D");
            break;
        default:
            System.out.println("some code for default");
    }
}

輸出:

some code for A

不,你不能使用有條件的switch

JAVA 7 允許Stringswitch case一起使用

為什么我不能打開字符串?

但條件不能與 switch 一起使用

您只能在 switch 中比較整個單詞。 對於您的場景,最好使用 if

還有哈希映射:

String SomeString = "gtgtdddgtgtg";

Map<String, Integer> items = new HashMap<>();
items.put("aaa", 0);
items.put("bbb", 1);
items.put("ccc", 2);
items.put("ddd", 2);

for (Map.Entry<String, Integer> item : items.entrySet()) {
    if (SomeString.contains(item.getKey())) {
        switch (item.getValue()) {
            case 0:
                System.out.println("do aaa");
                break;
            case 1:
                System.out.println("do bbb");
                break;
            case 2:
                System.out.println("do ccc&ddd");
                break;
        }
        break;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM