Java examples for java.lang:String Strip
collapse String Whitespace
//package com.java2s; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] argv) throws Exception { String s = "java2s.com"; System.out.println(collapseWhitespace(s)); }/*from ww w . j a va 2 s .c o m*/ public static final Pattern WHITESPACE_COLLAPSE_PATTERN1 = Pattern .compile("([\\r\\n\\t])"); public static final Pattern WHITESPACE_COLLAPSE_PATTERN2 = Pattern .compile("(\\s{2,})"); public static String collapseWhitespace(String s) { Matcher m1 = WHITESPACE_COLLAPSE_PATTERN1.matcher(s); s = m1.replaceAll(" "); Matcher m2 = WHITESPACE_COLLAPSE_PATTERN2.matcher(s); return m2.replaceAll(" "); } }