public static string StripSpaces( string value ) {
int len = value.Length;
if ( len <= 0 ) {
return string.Empty;
}
int startPos = 0;
StringBuilder norValue = null;
while ( value[startPos] == 0x20 ) {
startPos++;
if ( startPos == len ) {
return " ";
}
}
int i;
for ( i = startPos; i < len; i++ ) {
if ( value[i] == 0x20 ) {
int j = i + 1;
while ( j < len && value[j] == 0x20 ) {
j++;
}
if ( j == len ) {
if ( norValue == null ) {
return value.Substring( startPos, i - startPos );
}
else {
norValue.Append( value, startPos, i - startPos );
return norValue.ToString();
}
}
if ( j > i + 1 ) {
if ( norValue == null ) {
norValue = new StringBuilder( len );
}
norValue.Append( value, startPos, i - startPos + 1 );
startPos = j;
i = j - 1;
}
}
}
if ( norValue == null ) {
return ( startPos == 0 ) ? value : value.Substring( startPos, len - startPos );
}
else {
if ( i > startPos ) {
norValue.Append( value, startPos, i - startPos );
}
return norValue.ToString();
}
}