This can be achived by string Buffer,but just i tried out with String ;)
public static String replace(String totalString,String oldString,String newString){
//offset is 0,means we are serching oldString from index 0
int offset=0;
StringBuffer temp=new StringBuffer();
try{
while(totalString.indexOf(oldString,offset)!=-1&&offset< span=""><>
//the loop iterates until index is -1
//example:totalString:text ,oldString:ex newString:ax
temp.append(totalString.substring(offset, totalString.indexOf(oldString,offset)));
//temp has value ‘t’
temp.append(newString);//value of temp is ‘tax’
offset=totalString.indexOf(oldString,offset)+oldString.length();
}
if(offset< span=""><>
temp.append(totalString.substring(offset));
//value id temp is ‘taxt’
}
}catch (Exception e) {
System.out.println("Array out of bound of exception");
}
totalString=temp.toString();
return totalString;
}
No comments:
Post a Comment