Board index » delphi » first letter of each word 2 uCase?

first letter of each word 2 uCase?

I'm fairly new to Delphi and really enjoying it but I'm stuck to do
something which is easy 2 do in VB...

How do I change the first letter only for each word in a string to
uppercase?

Appreciate any help. Thanks in advance.
James Kelly

 

Re:first letter of each word 2 uCase?


Quote
"J. Kelly" <ja...@jskelly.co.uk> wrote in message news:8sel4t$isq$1@supernews.com...
> I'm fairly new to Delphi and really enjoying it but I'm stuck to do
> something which is easy 2 do in VB...

> How do I change the first letter only for each word in a string to
> uppercase?

> Appreciate any help. Thanks in advance.
> James Kelly

TheString[1] := UpCase(TheString[1]); // for a Pascal String

TheString[0] := UpCase(TheString[0]); // for an ASCIIZ string (array[0..whatever] of char)

Re:first letter of each word 2 uCase?


Don't know if its the best solution but you could use this:

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  AString: String;
  bUpCase: Boolean;
begin
  AString := 'test to set all uppercase';
  bUpCase := AString[1] in ['a'..'z', 'A'..'Z'];
  for I := 1 to Length(AString) do
  begin
    if bUpCase then AString[I] := UpCase(AString[I]);
    bUpCase := not (AString[I] in ['a'..'z', 'A'..'Z']);
  end;
  ShowMessage(AString);
end;

Hth,
    Lieven

Btw, you say it's standard in Vb?
Could you tell me the function that does it for Vb?

Quote
> How do I change the first letter only for each word in a string to
> uppercase?

Re:first letter of each word 2 uCase?


Thanks for the quick response, this works great for me.

BTW. Lieven, there is no actual standard function to do this in VB. I did it
in the past in VB by using the Mid function/statement to find the first
letter and the first letter after each space and convert to uppercase. If
you wish, I will search through my old code and post it for you.

Thanks again.
James Kelly.

Quote
Lieven Keersmaekers <Lieven.Keersmaek...@kava.be> wrote in message

news:6bBG5.72$dT.3670@nreader1.kpnqwest.net...
Quote
> Don't know if its the best solution but you could use this:

> procedure TForm1.Button1Click(Sender: TObject);
> var
>   I: Integer;
>   AString: String;
>   bUpCase: Boolean;
> begin
>   AString := 'test to set all uppercase';
>   bUpCase := AString[1] in ['a'..'z', 'A'..'Z'];
>   for I := 1 to Length(AString) do
>   begin
>     if bUpCase then AString[I] := UpCase(AString[I]);
>     bUpCase := not (AString[I] in ['a'..'z', 'A'..'Z']);
>   end;
>   ShowMessage(AString);
> end;

> Hth,
>     Lieven

> Btw, you say it's standard in Vb?
> Could you tell me the function that does it for Vb?

> > How do I change the first letter only for each word in a string to
> > uppercase?

Re:first letter of each word 2 uCase?


No need to dig it up. Know what you mean.
I was just curious because I thought you meant it
was standard in Vb and I really had no idea wich
function it would be :)
Glad I could help you.
Quote
> you wish, I will search through my old code and post it for you.

Re:first letter of each word 2 uCase?


In article <8sel4t$is...@supernews.com>, "J. Kelly" <ja...@jskelly.co.uk>
writes:

Quote
>How do I change the first letter only for each word in a string to
>uppercase?

function TitleCase(Str : string) : string;
var
  i : integer;
begin
  Result := Str;
  for i := 1 to Length(Result) - 1 do
    if (i = 1) then begin
      if Result[i] in ['a'..'z'] then
        Result[i] := Char(Ord(Result[i]) and not $20);  // uppercase the
character
      end
    else
      // set in next line contains characters preceeding character to uppercase

      if (Result[i] in ([' '..'~'] - ['-', 'A'..'Z', 'a'..'z'])) then  // see
note below
        if Result[i+1] in ['a'..'z'] then
          Result[i+1] := Char(Ord(Result[i+1]) and not $20);
end;

If you want the character after a hyphen to be capitalised then remove the '-',
from the above set.

Alan Lloyd
alangll...@aol.com

Re:first letter of each word 2 uCase?


Plenty of great code... thanks folks. I've searched lots and lots of Delphi
help sites and couldn't find a thing on this. I put a question here and
wow... a very short time I have several solutions!

Don't get me wrong, I'm not for a minute suggesting that folks like me
should just post here instead of searching the FAQ's everytime they get
stuck - that's what leads to multiple requests for the same solutions
several times per day. The FAQ's are also great for finding interesting code
and components too. Anyway, thanks again!

James Kelly

Re:first letter of each word 2 uCase?


Quote
J. Kelly wrote:
> Plenty of great code... thanks folks. I've searched lots and lots of
Delphi
> help sites and couldn't find a thing on this. I put a question here
and
> wow... a very short time I have several solutions!

And James, here's even more hints.

If you want to deal with letters other than a..z too, the upcase() won't
be enough.

The function below takes care of it.
Eg:
  "?? ?? ?? ?? ?? ?? "
converts to
  "?? ?? ?? ?? ?? ?? ".

{ uses Windows, SysUtils }
function AllLeadingCapitals(const S: string): string;
var
  Ix, Len: Integer;
begin
  if S = '' then Exit;
  Result := AnsiLowerCase(S);
  Len := Length(Result);
  Ix := 1;
  while Ix <= Len do
  begin
    while (Ix <= Len) and (not IsCharAlpha(Result[Ix])) do Inc(Ix);
    if Ix > Len then Exit;
    Result[Ix] := AnsiUpperCase(Result[Ix])[1];
    Inc(Ix);
    while (Ix <= Len) and (IsCharAlpha(Result[Ix])) do Inc(Ix);
  end;
end;

Regards
-ThomasN

Re:first letter of each word 2 uCase?


Fantastic! Thanks again folks for all the awesome code!

James Kelly.

Other Threads