두 문자열이 주어지면 두 문자열 중 하나가 다른 문자열의 맨 끝에 나타나고 대/소문자 차이를 무시하면 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 % 확신이 코드를 단순화 할 수있다,하지만 난 다른 논리 또는
이누군가가 내가 그것을 리팩토링하고 작게 만들 수 있습니다이 간단한 운동에 대해 생각하는 방법을 몰라?
이 코드는 [codereview.se] – MarioDS