code: public static String 
      trim( String string, int pos ) 
      {
          int len = string.length();
  
       	int leftPos = pos;
       	int rightPos = len;
       	
      	for( ; rightPos > 0; --rightPos ) 
      	{
      	    char ch = string.charAt( rightPos - 1 );
      		if( ch != ' '
      		    && ch != '\t' 
      		    && ch != '\n' 
      		    && ch != '\r' ) 
      		{    			
      			break;
      		}
      	}
      	
      	for( ; leftPos < rightPos; ++leftPos )
      	{
      	    char ch = string.charAt( leftPos ); 
      		if( ch != ' ' 
      		    && ch != '\t' 
      		    && ch != '\n' 
      		    && ch != '\r' ) 
      		{    			
      			break;
      		}
      	}
      	
      	if( rightPos <= leftPos ) 
      	{
      		return "";
      	}
      	
      	return ( leftPos == 0 && rightPos == string.length() )? string : 
      	                                                        string.substring( leftPos, rightPos );
      }
docs: Removes all whitespace characters from the beginning and
  end of the string starting from the given position.