Wierd String handling lazarus / 62-General ---------------------------------------------------------------------- J-G · 2022-03-11 00:25 I'm a stickler for good 'display' so in my latest programming effort which needs to display many Numeric Strings, once I've converted a number to a string to be used as a .Caption, I search for the location of a decimal point [Char(46)] and, if found, replace it with a Char(183). That works very well with simple delete & insert - viz. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Dot(S : string) : string;   // Change a decimal point to chr(250)Var  p : byte;begin  p := Pos('.',S);  if p>0 then    begin      delete(S,p,1);      Insert('·',S,p);    end;  Result := S;end;  However,  the end user may wish to edit the displayed value (read 'WILL' rather than 'may'!!) without re-typing the decimal point   -   ie. change   0·5  to  1·5  rather than 1.5  -  and naturally 1·5 is not acceptable for conversion to a 'float'.  That ought to be easy to handle in the same way - viz. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function DeDot(S : string): String;Var  p : byte;begin  p := Pos('·',S);  if p>0 then    begin      delete(S,p,1);      Insert('.',S,p);    end;  Result := S;end;  This correctly finds the Char(183) but neither delete or insert have any effect !   ie. the String comes back unaltered and that leads to an error  . I've also tried using 'Char(46)' rather than '.' - without success. ---------------------------------------------------------------------- winni · 2022-03-11 00:40 Hi! Your function fails definitly beyond 255. Strings can be up to 2^31-1 chars. And dont make it too complicate: Strings are just some special kind of array of char. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function DeDot(S : string): String;Var  p : Integer ; // !!!!!!!begin  p := Pos('·',S);  if p>0 then    begin    S[p] := '.';      end;  Result := S;end;   Winni ---------------------------------------------------------------------- J-G · 2022-03-11 02:49 Thanks for that Winnie  -  though the string will never be more than 10 characters long so the 255 char limit of byte won't be a problem. I had tried the S[p] := option but that put a '?' where the Dot should be. I've tried creating a 'Const'  called 'Point' and set that to Chr(46) but the DeDot still leaves S with a Chr(183) I've even tried taking LeftStr + '.' + RoghtStr  which when viewing within the function does show the decimal dot but upon return to the calling procedure the Chr(183) '·' has returned ! ---------------------------------------------------------------------- Josh · 2022-03-11 04:01 Hi would the stringreplace function do what you need stringreplace(MyString,'·','.',[rfReplaceAll, rfIgnoreCase]); Still confused for the user that the displayed number is not a number, as it is invalid, what if they tried copying pasting to/from another application.. However, I would hold all the strings as valid numerical strings and then have a routine that outputs the caption AirCode Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i constcfg_ConvertToCenterDot=True;cfg_ConvertToDecimal=False; function CenteredDot(AString:String;DecimalToCenteredDot:Boolean):String;   //  set DecimalToCenteredDot to true for decimal to dot or false for dot to decimalbegin  if DecimalToCenteredDot then stringreplace(AString,'.','·',[rfReplaceAll, rfIgnoreCase])  else stringreplace(AString,'·','.',[rfReplaceAll, rfIgnoreCase]);  result:= AString;end; label1.caption:=CenteredDot(MyCaptionString,true); or using constants for readability label1.caption:=CenteredDot(MyCaptionS tring,cfg_ConvertToCenterDot); ---------------------------------------------------------------------- MarkMLl · 2022-03-11 07:19 Is 183 a valie UTF-8 codepoint? I suspect that this will behave differently depending on how the character is specified and on whether it's String vs AnsiString. MarkMLl ---------------------------------------------------------------------- Thaddy · 2022-03-11 07:39 Yes, declare "string" specifically as AnsiString. Then it works. (Again an UTF8 fuck-up) Note it can be codepage dependent, because the upper half of ANSI (128-255) can differ, whereas UTF8 treats it different,AFAIK. ---------------------------------------------------------------------- jcmontherock · 2022-03-11 10:22 What I am using under Windows for editing numbers: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function EditNum(lValue: Double): String;var  fs: TFormatSettings;begin  result := '0';  if lValue = 0  then  exit;  GetLocaleFormatSettings(GetSystemDefaultLCID, fs);  fs.ThousandSeparator := ' ';  fs.DecimalSeparator  := '.';  result := FormatFloat(' ##,##', lValue, fs);end; Upper dot char seems not to be exist in UTF-8 (?). ---------------------------------------------------------------------- Thaddy · 2022-03-11 10:52 Quote from: jcmontherock on March 11, 2022, 11:22:49 amUpper dot char seems not to be exist in UTF-8 (?). Well, it DOES exist because UTF8 can express the full unicode range and some Ansi CP's can also express it. I forgot where and do not have the time now to find which cp's or which UTF8 codepoint, but surely it exists. ---------------------------------------------------------------------- J-G · 2022-03-11 11:03 Thanks for your input Josh - though it hasn't solved my problem  Quote from: josh on March 11, 2022, 05:01:27 amwould the stringreplace function do what you need stringreplace(MyString,'·','.',[rfReplaceAll, rfIgnoreCase]); I've tried this but it returns an empty string -- ie. wipes out all characters. Quote from: josh linkStill confused for the user that the displayed number is not a number, as it is invalid, what if they tried copying pasting to/from another application.. If the user fully enters the figure which includes a decimal point - whether they do it from keyboard or copy'n'paste there would be no problem since chr(183) wouldn't be in the string. The issue only raises its ugly head when a partial edit has been done such as changing the displayed            [ 0·5 ]   to  [ 1·5 ]  -  that is using my 'delete'/'insert' routine;  using stringreplace wipes the string completely. Quote from: josh linkHowever, I would hold all the strings as valid numerical strings and then have a routine that outputs the caption. Yes, I do that, but the user has to be able to enter new data which is into a TEdit which then needs to be evaluated, which I do by convering the TEdit.Caption to a Float, and the first thing I need to do is check for a Centred Dot [Chr(183)].  What I cannot fathom is why converting from Decimal to Dot works perfectly and the reverse doesn't. It surely cannot be down to some odd UTF8 behaviour. The characters 46 & 183 are both valid. ---------------------------------------------------------------------- J-G · 2022-03-11 11:14 Quote from: Thaddy on March 11, 2022, 11:52:21 amQuote from: jcmontherock on March 11, 2022, 11:22:49 amUpper dot char seems not to be exist in UTF-8 (?). Well, it DOES exist because UTF8 can express the full unicode range and some Ansi CP's can also express it. I forgot where and do not have the time now to find which cp's or which UTF8 codepoint, but surely it exists. The Chr(183) certainly does appear 'on screen' in the TEdit.Caption but in the Scource Editor (using Alt(0183) - on the keypad) displays a Capital A grave  -  I've become used to this, the Degree and Pound signs have similar 'issues'. ---------------------------------------------------------------------- Red_prig · 2022-03-11 11:17 symbol '·' as ANSI is $B7 but as utf8 is two byte symbol: $C2 $B7. And just as far as I remember, if it is not possible to decrypt the utf8 character, then a '?' is usually displayed ---------------------------------------------------------------------- MarkMLl · 2022-03-11 11:18 Quote from: J-G on March 11, 2022, 12:14:46 pmThe Chr(183) certainly does appear 'on screen' in the TEdit.Caption but in the Scource Editor (using Alt(0183) - on the keypad) displays a Capital A acute  -  I've become used to this, the Degree and Pound signs have similar 'issues'. That's primarily an OS issue. If in doubt use the character map provided by the IDE. MarkMLl ---------------------------------------------------------------------- wp · 2022-03-11 11:24 Quote from: J-G on March 11, 2022, 01:25:55 amI'm a stickler for good 'display' so in my latest programming effort which needs to display many Numeric Strings, once I've converted a number to a string to be used as a .Caption, "to be used as a .Caption" implies that you write a GUI application with Lazarus. This means: all strings are UTF-8 (unless you declare them differently, and I do not recommend to do this without a good reason). Let's look at your code in the 1st post from the viewpoint that S is encoded as UTF-8: Quote from: J-G on March 11, 2022, 01:25:55 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Dot(S : string) : string;   // Change a decimal point to chr(250)Var  p : byte;begin  p := Pos('.',S);  if p>0 then    begin      delete(S,p,1);      Insert('·',S,p);    end;  Result := S;end; This code is correct (except for the comment). '.' is an ASCII character (<128) and thus consists of 1 byte. "Delete" of 1 byte is correct. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function DeDot(S : string): String;Var  p : byte;begin  p := Pos('·',S);  if p>0 then    begin      delete(S,p,1);      Insert('.',S,p);    end;  Result := S;end; '·' is not ASCII (not < #128), and thus consists of more than one byte. Look at the Lazarus character map (menu "Edit"), you find it on the Range selection of "Latin-1 supplement", center of 2nd row; move the mouse over it and you can see that it contains the bytes C2 and B7. Therefore, the line "Delete(..., 1)" is not correct, you must delete two bytes, or, more general, Length('·'): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       delete(S,p,Length('·'));Pos and Insert, on the other hand are correct, because the first argument is a string anyway, and you do not make any assumptions on it. ---------------------------------------------------------------------- Thaddy · 2022-03-11 11:30 side note: if you are deleting/replacing multiple dots, always work backwards: for (high value).. downto. Don't know if you want that, so I called it a side note. ---------------------------------------------------------------------- J-G · 2022-03-11 12:14 SOLVED !! @WP  -  yes I had (re)discovered that (I'm sure I knew the more than one byte issue some years ago  ) but your explanation really put it in my mind! The basic problem was nothing to do with Delete/Insert not working at all (though the multi byte issue did need attention), It was down to my stupidity! (nothing new here then  ) I'd been calling two separate functions : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   DeDot(TEdit.Caption);  Number := StrToFloat(TEdit.Caption);  When I changed this to : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Number := StrToFloat(DeDot(TEdit.Caption));  ...all became sweetness and light.  Though I would appreciate a learnéd explanation as to the reason this version works! @Thaddy - a very valid observation but in this case there can only ever be one 'decimal point' in the string. Many thanks for all the input. ---------------------------------------------------------------------- wp · 2022-03-11 13:06 Be careful with the decimal separator. While it is a dot for you it is a comma for me! The StrToFloat and FloatToStr funtions use the FormatSettings.DecimalSeparator defined by the system settings, rather than a hard-coded dot ("."). If you want your code to work with all kinds of decimal separators you should replace the '.' in the code fragments above by FormatSettings.DecimalSeparator. (Unfortunately FormatSettings.DecimalSeparator is declared as a char and thus cannot be replaced by the centered dot (which is a string).) ---------------------------------------------------------------------- J-G · 2022-03-11 14:44 I would certainly agree with that caviat WP but the chance that my program will be used by anyone where the comma is considered the normal decimal separator is exeedingly slim  It's actually a vanity project concerned with measuring threads with the three wire method and seldom used outside highly specialised inspection departments. It only came about because I couldn't find any reference to calculating anything other than a 'approximation' and wanted to prove to myself that an accurate figure was attainable. It's certainly excercised my brain for the past week! ---------------------------------------------------------------------- Josh · 2022-03-11 15:45 hi J-G, apologies my code was flawed, forgot to assign the string correctly in the function  problem coding on the fly at stupid hours in the morning. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CenteredDot(AString:String;DecimalToCenteredDot:Boolean):String;   //  set DecimalToCenteredDot to true for decimal to dot or false for dot to decimalbegin  if DecimalToCenteredDot then result:=stringreplace(AString,'.','·',[rfReplaceAll, rfIgnoreCase])  else result:=stringreplace(AString,'·','.',[rfReplaceAll, rfIgnoreCase]);end;   attached a project which you may find usefull, enter your value, its pre-checked as its entered, you can also set the max amount of decimals you would allow. on entry it display the text field 'dotted' in label caption, then in next label shows the 'dedotted' of the 1st label; caption. ---------------------------------------------------------------------- MarkMLl · 2022-03-11 15:46 Quote from: J-G on March 11, 2022, 03:44:54 pmIt's actually a vanity project concerned with measuring threads with the three wire method and seldom used outside highly specialised inspection departments. Hands up anybody else who understands the gist of that :-) MarkMLl ---------------------------------------------------------------------- dsiders · 2022-03-11 18:12 Quote from: MarkMLl on March 11, 2022, 04:46:21 pmQuote from: J-G on March 11, 2022, 03:44:54 pmIt's actually a vanity project concerned with measuring threads with the three wire method and seldom used outside highly specialised inspection departments. Hands up anybody else who understands the gist of that :-) MarkMLl https://www.youtube.com/watch?v=LjQf6ISFlSg ---------------------------------------------------------------------- MarkMLl · 2022-03-11 18:27 Quote from: dsiders on March 11, 2022, 07:12:58 pmhttps://www.youtube.com/watch?v=LjQf6ISFlSg Don't think much of him: he was turning the thimble directly rather than using the clutch knob at the end. Or am I missing your point? MarkMLl ---------------------------------------------------------------------- dsiders · 2022-03-11 18:43 Quote from: MarkMLl on March 11, 2022, 07:27:41 pmQuote from: dsiders on March 11, 2022, 07:12:58 pmhttps://www.youtube.com/watch?v=LjQf6ISFlSg Don't think much of him: he was turning the thimble directly rather than using the clutch knob at the end. Or am I missing your point? MarkMLl No point. You asked what it was about. I posted a link. Other than finding the video, I know nothing about it... ---------------------------------------------------------------------- MarkMLl · 2022-03-11 18:47 Quote from: dsiders on March 11, 2022, 07:43:42 pmNo point. You asked what it was about. I posted a link. Other than finding the video, I know nothing about it... I know exactly what it's about thanks. I asked if anybody else did, since I'm not sure how many people with machinist and QA experience we've got in here (excluding OP, obviously). MarkMLl ---------------------------------------------------------------------- J-G · 2022-03-11 19:37 Quote from: josh on March 11, 2022, 04:45:11 pmhi J-G, apologies my code was flawed, forgot to assign the string correctly in the function  problem coding on the fly at stupid hours in the morning. I do most of my best work at 'stupid hours in the morning!' Thanks again for your input - I have run the program and confirm that it does exactly as required. I'll have to evaluate the concepts further of course to fully understand, but now I have a working solution it may take me some time! ---------------------------------------------------------------------- J-G · 2022-03-11 19:50 Quote from: MarkMLl on March 11, 2022, 04:46:21 pmQuote from: J-G on March 11, 2022, 03:44:54 pmIt's actually a vanity project concerned with measuring threads with the three wire method and seldom used outside highly specialised inspection departments. Hands up anybody else who understands the gist of that :-) MarkMLl Which part do you not understand Mark?  -  The 3 wire method?  see  https://littlemachineshop.com/images/gallery/instructions/ThreeWireMet hod.pdf ... or the 'vanity' The program will probably never see the real light of day but it's an interesting question which gives me personal kudos if I can solve it. All references I've found on the internet talk of 'approximations' and 'added constants' but I believe that accurate results can be obtained. EDIT :--  Ooops,  I posted that before I'd read further answers   That video was certainly not 'short' for the amount of information provided - and yes he ought to have used the clutch ! ---------------------------------------------------------------------- MarkMLl · 2022-03-11 20:10 Quote from: J-G on March 11, 2022, 08:50:17 pmThe program will probably never see the real light of day but it's an interesting question which gives me personal kudos if I can solve it. All references I've found on the internet talk of 'approximations' and 'added constants' but I believe that accurate results can be obtained. In which case it's definitely worth putting on Github or somewhere equivalent. MarkMLl