2013-05-12 67 views
-1

給定兩個字符串,如果任何一個字符串出現在另一個字符串的最後,則返回true,忽略大小寫差異(換句話說,計算不應該是「區分大小寫」)。注意:str.toLowerCase()返回字符串的小寫版本。重構這個長而簡單的代碼 - 字符串比較

endOther( 「Hiabc」, 「ABC」)→真

endOther( 「ABC」, 「HiaBc」)→真

endOther( 「ABC」, 「abXabc」)→真

public class endOtherClass{ 
     public static void main(String args[]) { 
      boolean is = endOther("yz","12xz") ; 
      System.out.println(is) ; 

     } 

    public static boolean endOther(String a, String b) { 

     a = a.toLowerCase() ; 
     b = b.toLowerCase() ; 

     if(a.length() == b.length()){ 
       System.out.println("a and b have same length!") ; 

       if(a.equals(b)){ 
        System.out.println("wow, even better - they are exact same") ; 
        return true ; 
          } 
       else { 
          System.out.println("but are diff!") ; 
          return false ; 
          } 
     } 

      String shorter = "" ;// figure out which - a or b - is shorter 
      String longer = "" ;// and which one is longer 

      if(a.length() > b.length()){ shorter = b ; longer = a ;} 
      else { shorter = a ; longer = b ;} 

      int offset = longer.length() - shorter.length() ; // the offset is used to know where exactly to start comparing 


      //go through the shorter and compare the corresponding part of the longer string 

      for(int i = 0 ; i < shorter.length() ; i++){ 
       System.out.println("comparing subs: " + shorter.substring(i,i+1) + " and " + longer.substring(offset+i, offset+i+1)) ; 


      if(!shorter.substring(i,i+1).equals(longer.substring(offset+i, offset+i+1))){ //we add offset so we can start comparing the last n characters of shorter string! 

        System.out.println("something wrong in long: " + longer.substring(offset+i, offset+i+1)) ; 

        System.out.println("something wrong in short: " + shorter.substring(i,i+1)) ; 
        return false ; 
       } 
      } 
      return true ; 

     } 


    } 

IM 90%肯定這個代碼可以簡化,但我不知道其他邏輯或思考這個簡單的練習

有人可以幫助我重構它,讓它更小的方式嗎?

+4

這可能更適合[codereview.se] – MarioDS 2013-05-12 10:34:33

回答

2

不要忘記檢查空情況。

public static boolean endOther(String a, String b) { 
    if(a == null || b == null) { 
     return false; 
    }  
    String lowerA = a.toLowerCase(); 
    String lowerB = b.toLowerCase(); 
    return lowerA.endsWith(lowerB) || lowerB.endsWith(lowerA); 
} 
+0

omg中得到一個例外,代碼太短了......感謝John Castle,這個解決方案是最優雅的 – ERJAN 2013-05-12 10:48:05

1

你可以使用"myString".toLowerCase().endsWith("ing") 代替循環你沒有使用正則表達式

1)正則表達式的方法

"abC".toLowerCase().matches("bc"+"$"); 

基本上BC

+0

爲什麼投票下來?這是否是jelousy? – Dima 2013-05-12 10:56:09

5
public static boolean endOther(String a, String b) { 

    return a.toLowerCase().endsWith(b.toLowerCase()) || 
      b.toLowerCase().endsWith(a.toLowerCase()); 
} 
+0

我只是想發佈endsWith解決方案。 – 11684 2013-05-12 10:37:44

+0

這必須是答案。好的解決方案= D – Jason 2013-05-12 10:49:18

+0

如果a或b爲空,該怎麼辦?你從代碼 – JohnCastle 2013-05-12 11:02:20

1

你可以簡單地重新因子代碼是正則表達式的一部分,因此美元符號表示此目標字符串必須以「bc」結尾。你可以改變這個「bc」來滿足你的需要。而「abC」是目標字符串。

2)字符串ENDWITH方法

悼念迪馬Goltsman

"myString".toLowerCase().endsWith("ing") 
0

你可以做這樣的事情:

public static boolean endOther(String a, String b) { 
    int minLength = Math.min(a.length, b.length); 
    a = a.substring(a.length - minLength).toLower(); 
    b = b.substring(b.length - minLength).toLower(); 
    return a.equals(b); 
} 

我不知道關於Java的語法,但你可以從這個代碼得到想法...