2013-05-12 2 views
-1

두 문자열이 주어지면 두 문자열 중 하나가 다른 문자열의 맨 끝에 나타나고 대/소문자 차이를 무시하면 true를 반환합니다. 즉, 계산이 " 대소 문자 구분 "). 참고 : str.toLowerCase()는 소문자 버전 문자열을 반환합니다.refactor이 길지만 단순한 코드 문자열

endOther ("Hiabc", "ABC")가 true

endOther → ("ABC", "HiaBc")가 true

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 ; 

     } 


    } 

메신저 90 % 확신이 코드를 단순화 할 수있다,하지만 난 다른 논리 또는

누군가가 내가 그것을 리팩토링하고 작게 만들 수 있습니다이 간단한 운동에 대해 생각하는 방법을 몰라?

+4

이 코드는 [codereview.se] – MarioDS

답변

2

null 사례를 확인하는 것을 잊지 마십시오.

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

mg에서 예외가 발생합니다. 코드가 너무 짧습니다. John Castle 덕분에이 솔루션이 가장 우아합니다. – ERJAN

1

당신은 "myString".toLowerCase().endsWith("ing") 대신 루프를 사용할 수 있습니다 당신은 정규식

1) 정규식 기본적으로 접근

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

BC를 사용하여

+0

에 더 적합한 이유는 무엇입니까? 그것은 열광적 인가요? – Dima

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

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

방금 ​​해결책을 게시 할 예정이었습니다. – 11684

+0

대답이어야합니다. 좋은 해결책 = D – Jason

+0

a 또는 b가 null 인 경우 어떻게됩니까? 코드 – JohnCastle

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

내가 자바 구문에 대한 확실하지 않다, 그러나 당신이 할 수있는 이 코드에서 아이디어를 얻으십시오 ...

관련 문제