Why not ignore the "replace" idea and simply create a new string with the same number of underscores...
String input = "Apple";
String output = new String(new char[input .length()]).replace("\0", "_");
//or
String output2 = StringUtils.repeat("_", input .length());
largely from here.
As many others have said, replaceAll
is probably the way to go if you don't want to include whitespace. For this you don't need the full power of regex but unless the string is absolutely huge it certainly wouldn't hurt.
//replaces all non-whitespace with "_"
String output3 = input.replaceAll("\S", "_");