?
Solved

Change first character of string into uppercase

Posted on 2004-10-14
11
Medium Priority
?
454 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:Stef Merlijn
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 6

Accepted Solution

by:
david_barker earned 1000 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
Technology Partners: 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:Stef Merlijn
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month16 days, 14 hours left to enroll

862 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