Solved

Change first character of string into uppercase

Posted on 2004-10-14
11
443 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…

832 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