Solved

Change first character of string into uppercase

Posted on 2004-10-14
11
444 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 317
Base1 Encode/Decode 3 81
CheckListBox usage 3 71
Breakpoint doesn't stop in my variable 3 30
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

860 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