Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { /** * Normalize a text string as per XPath normalize() function. * * @param s * The string to normalize. * @return The normalized text as character array. */ public static char[] normalizeText(String s) { char[] ch = s.trim().toCharArray(); if (ch.length == 0) { return ch; } return normalizeTrimmedText(ch); } private static char[] normalizeTrimmedText(char[] ch) { char[] ch2 = new char[ch.length]; boolean inWhitespace = false; int j = 0; for (int i = 0; i < ch.length; i++) { char c = ch[i]; boolean isWhitespace = Character.isWhitespace(c); if (isWhitespace && !inWhitespace) { ch2[j] = ' '; j++; inWhitespace = true; } else if (!isWhitespace) { ch2[j] = c; j++; inWhitespace = false; } } char[] ch3 = new char[j]; for (int i = 0; i < j; i++) { ch3[i] = ch2[i]; } return ch3; } }