Solved

Change first character of string into uppercase

Posted on 2004-10-14
11
445 Views
Last Modified: 2010-04-05
Hi,

I want to change the first position of a string into uppercase.
Also I don't know what happens when the first position in the string isn't a character. Maybe there must be a check if the 1st character is actually a character before changing it to uppercase.

Please supply me with a function.

Thanks for your help.
The Wiz
0
Comment
Question by:Delphiwizard
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 6

Accepted Solution

by:
david_barker earned 250 total points
ID: 12307191
No problem, even if the first character cannot be converted to uppercase, it will work.

procedure TForm1.Button1Click(Sender: TObject);
var
  sString  : string;
begin
  sString:='this is a string';
  sString:=UpperCase(Copy(sString,1,1))+Copy(sString,2,MaxInt);
end;
0
 
LVL 6

Expert Comment

by:Amir Azhdari
ID: 12307318
shorter way :

procedure TForm1.Button1Click(Sender: TObject);
var
  s  : string;
begin
  s:='this is a string';
  s[1]:=UpperCase(s[1])[1];
end;
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 12307371
Even quicker:

function MakeFirstCharUpper(s: string): string;
var c: pchar absolute s;
begin
  if (C^ >= 'a') and (C^ <= 'z') then Dec(C^, 32);
  result:=s;
end;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:pritaeas
ID: 12307407
Last function does not work for accented characters. Am not sure if UpperCase() does.
0
 
LVL 6

Expert Comment

by:Amir Azhdari
ID: 12307462
PierreC, :)
I think it's faster when we use uppercase func. because we don't need to test the first character if is Alphabet or no
0
 

Author Comment

by:Delphiwizard
ID: 12307505
Thanks to you all.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12307593
Fastest:

procedure FirstUppper(Var AString: String);
var Ptr: PChar;
begin
  Ptr := PChar(AString);
  if (Ptr^ <> #0) then Ptr^ := Upcase(Ptr^);
end;

You'll learn a lot when you create a competition to find the fastest StripChar function. ;-)
Unfortunately, it doesn't work when the value you're passing was declared with a default value, like: var S: string = 'oops';
The reason for this is related to the problem of string references, and the fact that the string is pointing to a read-only address.
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 12308240
@AmirAzhDari:
Uppercase actually does the same as may code plus some more, so my example would be the same as calling uppercase BUT faster.
0
 
LVL 2

Expert Comment

by:__alex
ID: 12319554
@Workshop_Alex
As far as I know the PChar call will make a writable copy of your string in case it's in a read only part of your memory.
To avoid copying you can use PChar(Pointer(AString[1])). Have a nice weekend!
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12320331
@Alex,
Since the parameter is of type VAR, it will crash if the original value is a writable constant since the original value is stored in a part of the memory that is read-only. My code version is also unsafe if the string is not unique. You should actually call
  UniqueString(AString);
before the rest of the code.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12331385
Only a little bit improvement: to work with Cyrillic languages (and may be with other languages) ....

function  UpperCaseFirstLetter(S: string): string;
var
  T:      string;
begin
  try
    if (S<>'') then
    begin
      T := S[1];
      T := ANSIUpperCase(T);
      S[1] := T[1];
    end;
  finally
    Result := S;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := UpperCaseFirstLetter(Edit1.Text);
end;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question