Recent

Author Topic: Delphi has 'function UpCaseFirst(AStr: String): String;'  (Read 7715 times)

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Delphi has 'function UpCaseFirst(AStr: String): String;'
« on: July 11, 2012, 11:16:40 pm »
Delphi has 'function UpCaseFirst(AStr: String): String;' I think this would be a good addition to Lazarus.  I looked for it, but didn't find it.  I wrote one quickly and it works, but I know it can be written better.

I checked it on English and it did what I needed.  I also checked to see what it would do with Hebrew because Hebrew does not have 'Case'.  It did nothing and that is what I hoped for.

function UpCaseFirst(AStr: AnsiString): AnsiString;
var
  S: AnsiString;
begin
  S:= LowerCase(AStr);
  S[1]:= UpCase(S[1]);
  Result:= S;
End;

Edit: I changed the code.  Better but still does not help for non-Ansi strings.
« Last Edit: July 12, 2012, 05:48:01 am by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1891
Re: Delphi has 'function UpCaseFirst(AStr: String): String;'
« Reply #1 on: July 12, 2012, 12:18:47 am »
It doesn't work with UTF-8. Try  UpCaseFirst('äpfel');   

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Delphi has 'function UpCaseFirst(AStr: String): String;'
« Reply #2 on: July 12, 2012, 12:28:17 am »
Thanks theo.  It's good to have your input.  By the way, what language is 'äpfel' and what does it mean?

That is why I mentioned the languages I checked it with and why I said the code could be written better.  I knew that it would not work with all languages.  I will try to improve the code tomorrow, but I still think Lazarus should have it.  It's very late, I'm going to bed :)
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Delphi has 'function UpCaseFirst(AStr: String): String;'
« Reply #3 on: July 12, 2012, 04:15:38 am »
theo, when I tried UpCase('äpfel') I got 'äPFEL'.  Is this correct?  It might be, but it looks wrong.  I have no idea about this language.  If it is not correct then maybe I don't feel so bad that my method does not work for that language.  But that does not make it right.

It looks like maybe UpCase and UpperCase also need some work as well.
« Last Edit: July 12, 2012, 04:28:30 am by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Delphi has 'function UpCaseFirst(AStr: String): String;'
« Reply #4 on: July 12, 2012, 05:23:22 am »
OK.  I did a little Googling and found that 'äpfel' is German for 'apple'.  I hope I got that right.  From what I could see,  UpCase('äpfel') should return 'Äpfel' and LowerCase('Äpfel') should return 'äpfel'.  Neither UpperCase nor LowerCase work that way.  They leave the 'Ä' and 'ä' unchanged.  The same with UpCase('äpfel').

Does anyone have any ideas?
Lazarus Trunk / fpc 2.6.2 / Win32

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Delphi has 'function UpCaseFirst(AStr: String): String;'
« Reply #5 on: July 12, 2012, 08:50:17 am »
Code: [Select]
uses ...,LazUTF8;

function UpCaseFirst(s:string):string;
var
  first:integer;
begin
  first:=UTF8CharacterLength(@(s[1]));
  result:=UTF8UpperCase(copy(s,1,first))+UTF8Lowercase(copy(s,first+1,length(s)));
end;


Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Delphi has 'function UpCaseFirst(AStr: String): String;'
« Reply #6 on: July 12, 2012, 12:24:35 pm »
ludob, that works like a charm :)  Thanks for your input and your time.  I checked against English, äpfel, and Hebrew and everything worked as expected.  I have already added it to my library.
« Last Edit: July 12, 2012, 12:46:48 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

 

TinyPortal © 2005-2018