indexof string java for replace code example Example: java replaceall single character .replaceAll("(?<!\\S)[^ ](?!\\S)", " ").trim()