Given a string, if one or both of the first 2 chars is 'x', return the string without those 'x' chars, and otherwise return the string unchanged. This is a little harder than it looks.

withoutX2("xHi") → "Hi"
withoutX2("Hxi") → "Hi"
withoutX2("Hi") → "Hi"

1
2
3
4
5
6
7
8
public String withoutX2(String str) {
  String result="";
  for (int i=0; i<=str.length()-1;i++){
    if ( !(i==0 || i==1) || !(str.substring(i,i+1).equals("x")) ){
      result=result+str.substring(i,i+1);
    }
  }return result;
}

Keresés

Ajánló