Board index » delphi » Capitalize the first letter of each word in a string

Capitalize the first letter of each word in a string

I am brand new to Delphi 2.0,

I would appreciate anyone that could post some sample code to set the
first character of every word in a string to uppercase and all other
letters to lowercase. This is identical to the Proper() function found
in MS or Lotus languages.

Thanks a lot in advance,

Tony Hart
t...@advernet.com

 

Re:Capitalize the first letter of each word in a string


Quote
Tony Hart wrote:

> I am brand new to Delphi 2.0,

> I would appreciate anyone that could post some sample code to set the
> first character of every word in a string to uppercase and all other
> letters to lowercase. This is identical to the Proper() function found
> in MS or Lotus languages.

> Thanks a lot in advance,

> Tony Hart
> t...@advernet.com

Here... If I understood you right, these ones do what you want...

function LowCase(ch : CHAR) : CHAR;
begin
  case ch of
    'A'..'Z' : LowCase := CHR (ORD(ch)+31);
  else
    LowCase := ch;
  end;
end;

function Proper (source, separators : STRING) : STRING;
var
  LastWasSeparator : BOOLEAN;
  ndx              : INTEGER;
begin
  LastWasSeparator := TRUE;  
  ndx := 1;
  while (ndx<=Length(source)) do begin
    if LastWasSeparator
    then source[ndx] := UpCase(source[ndx])
    else source[ndx] := LowCase(source[ndx]);
    LastWasSeparator := Pos(source[ndx], separators)>0;
    inc(ndx);
  end;
  Result := source;
end;

--
|----------------------------------------------------------------------------|
|Need a custom component? Late on a project? Could use an
util?              |
|DOS Device driver? A VxD? NT drivers or services? Applications of any
kind? |
|Low rates, fast
delivery!                                                   |
|----------------------------------------------------------------------------|
Erik Sperling Johansen <e...@info-pro.no>

Re:Capitalize the first letter of each word in a string


Quote
Tony Hart wrote:

> I am brand new to Delphi 2.0,

> I would appreciate anyone that could post some sample code to set the
> first character of every word in a string to uppercase and all other
> letters to lowercase. This is identical to the Proper() function found
> in MS or Lotus languages.

  I'm too tired to think properly, BUT, you'll want to do something like
this...   (I'm now emphasizing that I'm tired...)   ;)

function TMyForm.ChangeString(MyString: string): string;
var
  Temp          : string;
  i             : integer;
begin
  Mystring := LowerCase(Temp)
  for i := 1 to Length(Temp) do
    if Temp[i] = ' ' then Temp[i+1] := UpperCase(Temp[i+1]);
  Result := Temp;
end;

  Something like this (possibly with some minor changes) SHOULD do what
you want it to...  Good luck.

--
Jason
E...@DarkElf.reno.nv.us

Re:Capitalize the first letter of each word in a string


Quote
Tony Hart wrote:

> I am brand new to Delphi 2.0,

> I would appreciate anyone that could post some sample code to set the
> first character of every word in a string to uppercase and all other
> letters to lowercase. This is identical to the Proper() function found
> in MS or Lotus languages.

> Thanks a lot in advance,

> Tony Hart
> t...@advernet.com

Create a form with a edit, label and button control.
Attach this sample code to the click event of the button. I'm sure the
code could be improved, it's just something I knocked up to show you the
jist of how it's done.

procedure TForm1.Button1Click(Sender: TObject);
Var    c,x,y : string;
       z : integer;
       last_was_a_space : boolean;
begin
     x := edit1.text;
     y := '';
     last_was_a_space := false;

     for z:= 1 to length(x) do
     begin
          c := copy(x,z,1);
          if z = 1 then
             c := uppercase(c)
          else
              if last_was_a_space then c:= uppercase(c)
              else
                  c := lowercase(c);
          y := y + c;
          if c = ' ' then last_was_a_space := true
          else
              last_was_a_space := false;
     end;

     label1.caption := y;

end;

--
---------------------------------------
John Greening
Gloucestershire UK
---------------------------------------

Re:Capitalize the first letter of each word in a string


Quote
Erik Sperling Johansen wrote:
> =
> Tony Hart wrote:

> > I am brand new to Delphi 2.0,

> > I would appreciate anyone that could post some sample code to set the=
> > first character of every word in a string to uppercase and all other
> > letters to lowercase. This is identical to the Proper() function foun=

d

Quote
> function LowCase(ch : CHAR) : CHAR;
> begin
>   case ch of
>     'A'..'Z' : LowCase :=3D CHR (ORD(ch)+31);
>   else
>     LowCase :=3D ch;
>   end;
> end;

I can't believe you're from norway! :-) =

The above works fine on english characters A..Z but what =

about the others (in sweden we have =C5=C4=D6 for example)? =

Use the AnsiLowerCase function instead!

-- =

Svante Granqvist                 Speech, Music and Hearing
Phone +46-8-790 7561             Box 700 14 =

Fax   +46-8-790 7854             S-100 44 Stockholm
mailto:sva...@speech.kth.se      http://www.speech.kth.se/~svante

Re:Capitalize the first letter of each word in a string


   Here is a function that I use to capitalize the first character of each
word in a
Full Name field.  It capitalizes any character after a space, quote, double
quote, dash,
period or a left parenthesis.

Function  TfrmLoadProtocolTable.ToMixCase(InString: String): String;
Var I: Integer;
Begin
  Result := LowerCase(InString);
  Result[1] := UpCase(Result[1]);
  For I := 1 To Length(InString) - 1 Do Begin
    If (Result[I] = ' ') Or (Result[I] = '''') Or (Result[I] = '"')
    Or (Result[I] = '-') Or (Result[I] = '.')  Or (Result[I] = '(') Then
      Result[I + 1] := UpCase(Result[I + 1]);
  End;
End;

--
                                                            Cleon.

Tony Hart <t...@advernet.com> wrote in article
<3305EAEB....@advernet.com>...

Quote
> I am brand new to Delphi 2.0,

> I would appreciate anyone that could post some sample code to set the
> first character of every word in a string to uppercase and all other
> letters to lowercase. This is identical to the Proper() function found
> in MS or Lotus languages.

> Thanks a lot in advance,

> Tony Hart
> t...@advernet.com

Re:Capitalize the first letter of each word in a string


In article <01bc1c36$c4fa9720$124381d0@loco>, "Cleon T. Bailey"
<baile...@ionet.net> writes

Quote
>   Here is a function that I use to capitalize the first character of each
>word in a
>Full Name field.  It capitalizes any character after a space, quote, double
>quote, dash,
>period or a left parenthesis.

>Function  TfrmLoadProtocolTable.ToMixCase(InString: String): String;
>Var I: Integer;
>Begin
>  Result := LowerCase(InString);
>  Result[1] := UpCase(Result[1]);

=======================================================================
Quote
>  For I := 1 To Length(InString) - 1 Do Begin
>    If (Result[I] = ' ') Or (Result[I] = '''') Or (Result[I] = '"')
>    Or (Result[I] = '-') Or (Result[I] = '.')  Or (Result[I] = '(') Then
>      Result[I + 1] := UpCase(Result[I + 1]);
>  End;

=======================================================================

Quote
>End;

Or (IMHO) slightly more readable:

  For I := 1 To Length(InString) - 1 Do
    If Result[I] in [' ', '''', '"', '-', '.', '('] then
      Result[I + 1] := UpCase(Result[I + 1]);

--
Tim Stannard -  Ad Hoc Computing, Addlestone, Surrey, UK
                Tel/Fax: 01932 830572  Mob: 0589 918184
                e-mail: stann...@adhoc.demon.co.uk

Re:Capitalize the first letter of each word in a string


Both Tim Stannard's and Cleon T. Bailey's functions will bomb in D2 if sent
an empty string (where accessing InString[1] causes an access violation,
the second attempt will do the same if the last character is in the set.

try this instead:

function proper(s:string):string;
var t:string;
    i:integer;
    newWord:boolean;
begin
if s='' then exit;
s:=lowercase(s);
t:=uppercase(s);
newWord:=true;
for i:=1 to length(s) do
    begin
    if newWord and (s[i] in ['a'..'z']) then
       begin s[i]:=t[i]; newWord:=false; continue; end;
    if s[i] in ['a'..'z',''''] then continue;
    newWord:=true;
    end;
result:=s;
end;

HTH,

--
Paul Motyer
pa...@pccity.com.au - now working!
Soft Stuff, Croydon, Australia, 3136

Tim Stannard <T...@adhoc.demon.co.uk> wrote in article
<7CCtENAMCNCzE...@adhoc.demon.co.uk>...

Quote
> In article <01bc1c36$c4fa9720$124381d0@loco>, "Cleon T. Bailey"
> <baile...@ionet.net> writes
> >   Here is a function that I use to capitalize the first character of
each
> >word in a
> >Full Name field.  It capitalizes any character after a space, quote,
double
> >quote, dash,
> >period or a left parenthesis.

> >Function  TfrmLoadProtocolTable.ToMixCase(InString: String): String;
> >Var I: Integer;
> >Begin
> >  Result := LowerCase(InString);
> >  Result[1] := UpCase(Result[1]);

> =======================================================================
> >  For I := 1 To Length(InString) - 1 Do Begin
> >    If (Result[I] = ' ') Or (Result[I] = '''') Or (Result[I] = '"')
> >    Or (Result[I] = '-') Or (Result[I] = '.')  Or (Result[I] = '(') Then
> >      Result[I + 1] := UpCase(Result[I + 1]);
> >  End;
> =======================================================================
> >End;

> Or (IMHO) slightly more readable:

>   For I := 1 To Length(InString) - 1 Do
>     If Result[I] in [' ', '''', '"', '-', '.', '('] then
>       Result[I + 1] := UpCase(Result[I + 1]);

> --
> Tim Stannard -  Ad Hoc Computing, Addlestone, Surrey, UK
>                 Tel/Fax: 01932 830572  Mob: 0589 918184
>                 e-mail: stann...@adhoc.demon.co.uk

Other Threads