Saturday, May 3, 2014

Use of StringBuilder() and deleteCharAt

Ques:

Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..str.length()-1 inclusive).

Ans: 


public String missingChar(String str, int n) {
  StringBuilder sb=new StringBuilder(str);
   if(n<=sb.length()-1){
        return sb.deleteCharAt(n).toString();}else{return sb.toString();}
}

No comments:

Post a Comment