Solved

Change first character of string into uppercase

Posted on 2004-10-14
11
439 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now