Here you can find the source of normalizeSpaces(char[] buf, int origStart, int origEnd)
Parameter | Description |
---|---|
buf | Buffer that contains the String to check |
origStart | Offset of the first character of the text to check in the buffer |
origEnd | Offset of the character following the last character of the text (as per usual Java API convention) |
public static String normalizeSpaces(char[] buf, int origStart, int origEnd)
//package com.java2s; //License from project: Apache License public class Main { final static char CHAR_SPACE = ' '; /**//from w w w. j a va 2 s . c o m * Method that will check character array passed, and remove all * "extra" spaces (leading and trailing space), and normalize * other white space (more than one consequtive space character * replaced with a single space). *<p> * NOTE: we only remove explicit space characters (char code 0x0020); * the reason being that other white space must have come from * non-normalizable sources, ie. via entity expansion, and is thus * not to be normalized * * @param buf Buffer that contains the String to check * @param origStart Offset of the first character of the text to check * in the buffer * @param origEnd Offset of the character following the last character * of the text (as per usual Java API convention) * * @return Normalized String, if any white space was removed or * normalized; null if no changes were necessary. */ public static String normalizeSpaces(char[] buf, int origStart, int origEnd) { --origEnd; int start = origStart; int end = origEnd; // First let's trim start... while (start <= end && buf[start] == CHAR_SPACE) { ++start; } // Was it all empty? if (start > end) { return ""; } /* Nope, need to trim from the end then (note: it's known that char * at index 'start' is not a space, at this point) */ while (end > start && buf[end] == CHAR_SPACE) { --end; } /* Ok, may have changes or not: now need to normalize * intermediate duplicate spaces. We also now that the * first and last characters can not be spaces. */ int i = start + 1; while (i < end) { if (buf[i] == CHAR_SPACE) { if (buf[i + 1] == CHAR_SPACE) { break; } // Nah; no hole for these 2 chars! i += 2; } else { ++i; } } // Hit the end? if (i >= end) { // Any changes? if (start == origStart && end == origEnd) { return null; // none } return new String(buf, start, (end - start) + 1); } /* Nope, got a hole, need to constuct the damn thing. Shouldn't * happen too often... so let's just use StringBuilder() */ StringBuilder sb = new StringBuilder(end - start); // can't be longer sb.append(buf, start, i - start); // won't add the starting space while (i <= end) { char c = buf[i++]; if (c == CHAR_SPACE) { sb.append(CHAR_SPACE); // Need to skip dups while (true) { c = buf[i++]; if (c != CHAR_SPACE) { sb.append(c); break; } } } else { sb.append(c); } } return sb.toString(); } }