* * *

Author Topic: How to uppercase individual character in string  (Read 1538 times)

Romain

  • New member
  • *
  • Posts: 15
    • Attribute Changer Website
How to uppercase individual character in string
« on: December 30, 2017, 11:57:43 pm »
I'm trying to convert a Delphi application to Lazarus/FPC and so far made very good progress. Using FPC 3.0.4 and Lazarus 1.8 ( 64-bit ) on Windows 10.

However, in one of my functions I need to convert the first character of each word to uppercase and it fails. I did the following tests on Windows 10 :

var L_String: string;   ( AnsiString enabled in settings )

L_String := 'öhh';   

ShowMessage(AnsiProperCase(L_String,StdWordDelims));     -> Application crashes ( project raised exception class 'External: SIGSEGV')
ShowMessage(AnsiUpperCase(L_String));  -> displays 'ÖHH' as expected
ShowMessage(AnsiUpperCase(L_String[1]));  -> displays a BLANK in the dialog box
ShowMessage(AnsiUpperCase(L_String[2]));  -> displays a BLANK in the dialog box

I really don't find a solution and any help is really appreciated.

Romain

hayanninja

  • New member
  • *
  • Posts: 34
Re: How to uppercase individual character in string
« Reply #1 on: December 31, 2017, 12:20:28 am »
AnsiUpperCase(L_String[1] + L_String[2]) gives the intended effect.

This suggests to me that ö might not be completely within AnsiString's capabilities (I have a feeling AnsiString might actually only handle characters $00 to $7F) and you may want to consider *not* using AnsiStrings for your project...

However, the last two lines WOULD work fine on a character that is within AnsiString's character set.

Romain

  • New member
  • *
  • Posts: 15
    • Attribute Changer Website
Re: How to uppercase individual character in string
« Reply #2 on: December 31, 2017, 01:16:13 am »
Thank you for your quick reply.

I wonder what string datatype I should use. Even UnicodeString seems to have issues and produces garbage :-/

L_String : UnicodeString;

L_String := 'öhh';

label1.caption := UpperCase(L_String) outputs 'öHH'

I read through the documentation and it's not easy to find the correct information. I thought that all strings in FPC and the LCL were UTF8 enabled.

engkin

  • Hero Member
  • *****
  • Posts: 1972
Re: How to uppercase individual character in string
« Reply #3 on: December 31, 2017, 01:22:31 am »
Use String, but learn Unicode.

By default Lazarus uses UTF8 where letter
o is one byte.
ö is more than one byte (two in this case).

However it is possible to write ö using o + ¨ which enhances our skills(?).

Notice how you can write the same "letter" using two different ways.
Also notice how UTF8 uses variable number if bytes to represent different "letters"
« Last Edit: December 31, 2017, 01:30:17 am by engkin »

Romain

  • New member
  • *
  • Posts: 15
    • Attribute Changer Website
Re: How to uppercase individual character in string
« Reply #4 on: December 31, 2017, 01:28:00 am »
I use String as mentioned in my first post.

But I fail to get it working. Do you have any idea how to solve my problem ?

Any input is welcome :-)

Romain

  • New member
  • *
  • Posts: 15
    • Attribute Changer Website
Re: How to uppercase individual character in string
« Reply #5 on: December 31, 2017, 01:41:59 am »
I know that UTF8 has variable representations for different characters.

However I do not understand how this works in FPC. Do I need to use LazUTF8 ?

Thanks four your help. Maybe you have a link to some documentation.

jamie

  • Hero Member
  • *****
  • Posts: 709
Re: How to uppercase individual character in string
« Reply #6 on: December 31, 2017, 03:36:01 am »
if this gives you any clarity ..
Code: Pascal  [Select]
  1. procedure TForm1.Button8Click(Sender: TObject);
  2. Var
  3.    L:String;
  4.    H:String;
  5.    I:Integer;
  6. begin
  7.    L := 'öhh';
  8.    I := 1;
  9.    While (ord(L[I])> 127)and(I <= Length(L)) do Begin H := H+L[I];inc(I);end;
  10.    Caption := AnsiUpperCase(H);
  11. end;                                            

I sucked out the characters that were above 127, combined them to form a String for a
single char.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 697
Re: How to uppercase individual character in string
« Reply #7 on: December 31, 2017, 07:35:40 am »
hello,
with the wp function UTF8UppercaseFirstChar
Code: Pascal  [Select]
  1. uses
  2.   LazUTF8;
  3.  
  4. function UTF8UppercaseFirstChar(s: String): String;
  5. var
  6.   ch, rest: String;
  7. begin
  8.   ch := UTF8Copy(s, 1, 1);
  9.   rest := Copy(s, Length(ch)+1, MaxInt);
  10.   Result := UTF8Uppercase(ch) + rest
  11. end;  

with this code :
Code: Pascal  [Select]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var L_String: string;
  3. begin
  4. L_String := 'öhé ! Bonne année (en français) ! ';
  5. Label1.Caption := UTF8UppercaseFirstChar(L_String);
  6. end;    

it seems to be OK (see attachment) on Windows 7 Lazarus 1.8  32 bits . Font for label : SegoUI

Friendly, J.P

Romain

  • New member
  • *
  • Posts: 15
    • Attribute Changer Website
Re: How to uppercase individual character in string
« Reply #8 on: December 31, 2017, 09:20:40 am »
Dear all,

Many thanks for your help. As a newcomer to FPC and Lazarus, I really appreciate this great community !

I also found this link and it gave me in insight in UTF and Lazarus http://wiki.freepascal.org/UTF8_strings_and_characters

Romain

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3366
  • I like bugs.
Re: How to uppercase individual character in string
« Reply #9 on: December 31, 2017, 09:58:40 am »
In r56892 I added function UTF8ProperCase() to unit LazUTF8.
Unfortunately it cannot be mapped to AnsiProperCase() in Lazarus Unicode system like some other Ansi...() functions.
It uses pointers like the original AnsiProperCase() does. It uses System.Move() twice for capital letters which looks clumsy but it should be quite fast also with long strings.

Romain

  • New member
  • *
  • Posts: 15
    • Attribute Changer Website
Re: How to uppercase individual character in string
« Reply #10 on: December 31, 2017, 10:07:36 am »
Great news  :)

Many thanks JuhaManninen !

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus