Logo Search packages:      
Sourcecode: icu version File versions  Download package

U_NAMESPACE_BEGIN UnicodeString & UnicodeString::trim ( void   ) 

Trims leading and trailing whitespace from this UnicodeString.

Returns:
a reference to this ICU 2.0

Definition at line 26 of file unistr_props.cpp.

References isBogus(), length(), U16_NEXT, and U16_PREV.

Referenced by ChoiceFormat::applyPattern(), MessageFormat::findKeyword(), and UnicodeStringTest::TestSpacePadding().

{
  if(isBogus()) {
    return *this;
  }

  UChar *array = getArrayStart();
  UChar32 c;
  int32_t oldLength = this->length();
  int32_t i = oldLength, length;

  // first cut off trailing white space
  for(;;) {
    length = i;
    if(i <= 0) {
      break;
    }
    U16_PREV(array, 0, i, c);
    if(!(c == 0x20 || u_isWhitespace(c))) {
      break;
    }
  }
  if(length < oldLength) {
    setLength(length);
  }

  // find leading white space
  int32_t start;
  i = 0;
  for(;;) {
    start = i;
    if(i >= length) {
      break;
    }
    U16_NEXT(array, i, length, c);
    if(!(c == 0x20 || u_isWhitespace(c))) {
      break;
    }
  }

  // move string forward over leading white space
  if(start > 0) {
    doReplace(0, start, 0, 0, 0);
  }

  return *this;
}


Generated by  Doxygen 1.6.0   Back to index