1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] argv) throws Exception { String ExString = "This is a Java program. This is another Java Program."; String result=removeDuplicateWhitespace(ExString); System.out.println(result); } public static CharSequence removeDuplicateWhitespace(CharSequence inputStr) { String patternStr = "\\s+"; String replaceStr = " "; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); return matcher.replaceAll(replaceStr); } } |