Capitalizing first characters of every word
   


This function will take a string, lowercase it, and uppercase the first characters of all the words in the string. For ex.: 'the quick brown fox' will become 'The Quick Brown Fox'. Thanks to Sanford Aranoff for this code.

function Capitalize(const Line1: string): string;
{ Func to capitalize the first char of every word.        }
{ Code adapted from Sanford Aranoff <[email protected]> }

const
  period = '.';
  comma = ',';
  slash = '/';
  bslash = '\';
  blank = ' ';
  set_let_next = [period,'-'];
  set_let_prev = [period,slash,bslash,'-',comma,'"'];
var
   let: char;
   i, j: integer;
   test: boolean;

  function  LineIsNull(const Source: string): boolean;
  {Determine if a string contains only char. 0-32.}

  asm
      Push  ESI
            //save the important stuff
      Mov   @Result,true
      Or    EAX,EAX

      Jz    @Done
          //abort if nil address
      Mov   ESI,EAX
        //put address into read register
      Mov   ECX,[EAX-4]
    //put length into count register
      Jecxz @NG
            //bail out if zero length
      Cld
                  //make sure we go forward
    @Start:

      Lodsb
                //get a byte
      Cmp   AL,32
          //greater than space?
      Ja    @NG
            //yes, then abort
      Dec   ECX
            //do it again
      Jnz   @Start

      Mov   EAX,-1
         //if we make it here, it's a null string
      Jmp   @Done
    @NG:

      xor   EAX,EAX

      Mov   @Result,false

    @Done:

      Pop   ESI
           //restore the important stuff
  end;

begin
  if LineIsNull(line1) then
    begin
      Result:= '';
      Exit;
    end;

  Result := Trim(Line1);
  i := Length(Result);
  if i = 1 then
    begin
      Result[1] := UpCase(Result[1]);
      Exit;
    end;

  Result := LowerCase(Result);
  Result[1] := UpCase(Result[1]);
  j := 1;
  repeat
    Let := Result[j];
    inc(j);
    Test := (Let <= blank) or (Let in set_let_prev);
    if not test then
      begin
        if j > 2 then
          Let := Result[j-2]
        else
          Let := Blank;
        Test := (Let <= blank) or (Let in set_let_prev);
        if test then
          begin
            if j < i then
              begin
                if j <= 2 then
                  Test := Result[j+1] in [Period ,'-', Blank]
                else
                  Test := Result[j+1] in set_let_next;
              end
            else
              Test := False;
          end;
      end;
    if Test then
      Result[j] := UpCase(Result[j]);
  until
    j = i;
end;


Blue Orb Software
[email protected]