public static String cleanWhitespace(String theResult) {
		StringBuilder b = new StringBuilder();
		boolean inWhitespace = false;
		boolean betweenTags = false;
		boolean lastNonWhitespaceCharWasTagEnd = false;
		boolean inPre = false;
		for (int i = 0; i < theResult.length(); i++) {
			char nextChar = theResult.charAt(i);
			if (inPre) {
				b.append(nextChar);
				continue;
			} else if (nextChar == '>') {
				b.append(nextChar);
				betweenTags = true;
				lastNonWhitespaceCharWasTagEnd = true;
				continue;
			} else if (nextChar == '\n' || nextChar == '\r') {
				continue;
			}

			if (betweenTags) {
				if (Character.isWhitespace(nextChar)) {
					inWhitespace = true;
				} else if (nextChar == '<') {
					if (inWhitespace && !lastNonWhitespaceCharWasTagEnd) {
						b.append(' ');
					}
					b.append(nextChar);
					inWhitespace = false;
					betweenTags = false;
					lastNonWhitespaceCharWasTagEnd = false;
					if (i + 3 < theResult.length()) {
						char char1 = Character.toLowerCase(theResult.charAt(i + 1));
						char char2 = Character.toLowerCase(theResult.charAt(i + 2));
						char char3 = Character.toLowerCase(theResult.charAt(i + 3));
						char char4 = Character.toLowerCase((i + 4 < theResult.length()) ? theResult.charAt(i + 4) : ' ');
						if (char1 == 'p' && char2 == 'r' && char3 == 'e') {
							inPre = true;
						} else if (char1 == '/' && char2 == 'p' && char3 == 'r' && char4 == 'e') {
							inPre = false;
						}
					}
				} else {
					lastNonWhitespaceCharWasTagEnd = false;
					if (inWhitespace) {
						b.append(' ');
						inWhitespace = false;
					}
					b.append(nextChar);
				}
			} else {
				b.append(nextChar);
			}
		}
		return b.toString();
	}
public static String cleanWhitespace ( String theResult ) { StringBuilder b = new StringBuilder ( ) ; boolean inWhitespace = false ; boolean betweenTags = false ; boolean lastNonWhitespaceCharWasTagEnd = false ; boolean inPre = false ; for ( int i = 0 ; i < theResult . length ( ) ; i ++ ) { char nextChar = theResult . charAt ( i ) ; if ( inPre ) { b . append ( nextChar ) ; continue ; } else if ( nextChar == '>' ) { b . append ( nextChar ) ; betweenTags = true ; lastNonWhitespaceCharWasTagEnd = true ; continue ; } else if ( nextChar == '\n' || nextChar == '\r' ) { continue ; } if ( betweenTags ) { if ( Character . isWhitespace ( nextChar ) ) { inWhitespace = true ; } else if ( nextChar == '<' ) { if ( inWhitespace && ! lastNonWhitespaceCharWasTagEnd ) { b . append ( ' ' ) ; } b . append ( nextChar ) ; inWhitespace = false ; betweenTags = false ; lastNonWhitespaceCharWasTagEnd = false ; if ( i + 3 < theResult . length ( ) ) { char char1 = Character . toLowerCase ( theResult . charAt ( i + 1 ) ) ; char char2 = Character . toLowerCase ( theResult . charAt ( i + 2 ) ) ; char char3 = Character . toLowerCase ( theResult . charAt ( i + 3 ) ) ; char char4 = Character . toLowerCase ( ( i + 4 < theResult . length ( ) ) ? theResult . charAt ( i + 4 ) : ' ' ) ; if ( char1 == 'p' && char2 == 'r' && char3 == 'e' ) { inPre = true ; } else if ( char1 == '/' && char2 == 'p' && char3 == 'r' && char4 == 'e' ) { inPre = false ; } } } else { lastNonWhitespaceCharWasTagEnd = false ; if ( inWhitespace ) { b . append ( ' ' ) ; inWhitespace = false ; } b . append ( nextChar ) ; } } else { b . append ( nextChar ) ; } } return b . toString ( ) ; }
Trims the superfluous whitespace out of an HTML block