Given a string of odd length, return length three from its middle- replaceALL

java replaceAll() method with example program. This kind of question are asked in codingbat. Improve your programming skills by solving this, its for beginners. Program with explanation.

public String middleThree(String str)

{

String newstr;

int mid;

str=str.replaceAll(“\\s” , “”);

int len=str.length();

if(len==3)

{

return str;

}

mid=(len/2)-1;

newstr=str.substring(mid,mid+3);

return newstr;

}

Below program explain java replace() method. Syntax, example and explanation.

Program using replace method