Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Pascal - How to capitalize each word in a string

6 Posts
2 Users
0 Reactions
5,801 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

This is a small function I use for capitalizing each word in a string - obviously this works in most Pascal version. I use it in Delphi and Lazarus.

function Capitalize (const s: string): string;
var flag: boolean;
i : Byte;
t : string;
begin
if s<>'' then
begin
flag := true;
t := '';
for i := 1 to Length(s) do
begin
if flag then
t := t+ UpperCase(s)
else
t := t+ s;
flag := (s = ' ')
end;
end
else t:='';
result := t;
end;

   
ReplyQuote
(@bluegyn)
New Member
Joined: 11 years ago
Posts: 2
 

The same fixed for Delphi, (char s[ i ] instead of string s ) , and added the - separator for capitalizing fistnames like anna-maria => Anna-Maria   

Good job ***{:-

Note that your editor robot, automatiquely transforms the correct s [ i ] syntax in incorrect s ...

 function Capitalize(const s: string): string;
    { //www.tweaking4all.com/forums/topic/pascal-how-to-capitalize-each-word-in-a-string/ }
    var
      flag: Boolean;
      I:    Byte;
      t:    string;
    begin
      if s <> '' then
        begin
          flag  := true;
          t     := '';
          for I := 1 to Length(s) do
            begin
              if flag then
                t := t + UpperCase(s [ I ] )
              else
                t  := t + LowerCase(s [ I ] );
              flag := ((s [ I ] = ' ') or (s [ I ] = '-'))
            end;
        end
      else
        t    := '';
      result := t;
    end;


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

Thanks bluegyn for catching that ! 
Yeah, the editor and the backend have their issues ... I'm sorry about that.
It's interesting, because I just tried to edit the original post and guess what .. the [ i ] show correctly in the editor, just not when it's displayed as a post ..   .


   
ReplyQuote
(@bluegyn)
New Member
Joined: 11 years ago
Posts: 2
 

The way found to fix this display bug was to add a space to the bracket like this [ i ]

Found a soluce => The Double Bracket (like double quoting tu put a quote in a Pascal string)

- You write :  TEST [ [ i ] ]
- You get :TEST []


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

Thanks again - I've been searching all morning to see if I can find a replacement editor or forum ... I've tried several editors and they all have been a pain ... not to mention that the backend keeps "modifying" content. 

Thanks for the tip though! 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

Managed to fix the issue ... it was a bbcode plugin that interpreted that as tag for setting text to italic ...

I really love the forum I'm using, except for the poor editor support 


   
ReplyQuote
Share: