[Solved] Remove leading Zero lazarus / 62-General ---------------------------------------------------------------------- folkeu08 · 2022-03-12 08:52 Hi, I have an example for Delphi with the " JEDI Code Library" : 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 uses JclStrings; var  S: string; begin  S := StrTrimCharLeft('00000004357816', '0');end.  I don't know if this fonction existe in Jvcllaz and which library to declare. I've looked in the component sources but can't find anything. Is this present in JVCLLaz ? Thanks ---------------------------------------------------------------------- winni · 2022-03-12 09:45 Hi! Do it yourself: 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 While (Length(s)>0) and (s[1] ='0') do delete (s,1,1); Winni ---------------------------------------------------------------------- wp · 2022-03-12 09:45 Not 100% but I don't think so. But there is a similar function in unit StrUtils: 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 TrimLeftSet(const S: String;const CSet:TSysCharSet): String; So, you can achieve the same effect by calling 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 s := TrimLeftSet(S, ['0']); ---------------------------------------------------------------------- Thaddy · 2022-03-12 10:50 Alternatively: 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 {$mode objfpc}{$H+}uses sysutils;begin  writeln('000000abcde'.trimleft(['0'])); // use the helper from sysutils;end. ---------------------------------------------------------------------- Kays · 2022-03-12 11:28 LMDDGTFY. → some solutions. However, usually “remove leading zero” is understood to leave one zero if the numeric value is zero. The examples provided so far will remove all zeros on the left-hand side, so you might end up with an empty string. I’d suggest to just use readStr [or similar] if you intend to use the numeric value anyway.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 program removeLeadingZero(output);var        i: integer;      s: string(20);begin        readStr('00000004357816', i);        { use `i` }        writeStr(s, i:1);                writeLn(s)end. ---------------------------------------------------------------------- winni · 2022-03-12 12:48 Quote from: Kays on March 12, 2022, 12:28:04 pm However, usually “remove leading zero” is understood to leave one zero if the numeric value is zero. The examples provided so far will remove all zeros on the left-hand side, so you might end up with an empty string.  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 While (Length(s)>1) and (s[1] ='0') do delete (s,1,1); Winni ---------------------------------------------------------------------- Thaddy · 2022-03-12 13:05 @both Kays and winni: The jcl version also removes all zero's, although I see the logic to leave one (better insert one if empty) if and only if the string represents a numeric, which is not the case in OP's question..... An observation is, that the JCL code is a lot worse than many of the implementations above  It shows its age. ---------------------------------------------------------------------- munair · 2022-03-12 19:58 Why not a simple conversion to numeric and back: 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 s := '00000004357816';s := IntToStr(StrToInt(s)); No loops, no need to insert a zero when zero and only a one-time string reassignment. ---------------------------------------------------------------------- winni · 2022-03-13 00:19 Quote from: munair on March 12, 2022, 08:58:14 pmWhy not a simple conversion to numeric and back: 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 s := '00000004357816';s := IntToStr(StrToInt(s)); No loops, no need to insert a zero when zero and only a one-time string reassignment. Not only one loop but two. Look inside of IntToStr and StrToInt. Winni ---------------------------------------------------------------------- Josh · 2022-03-13 01:55 hi and some overkill, can remove whitespace, can add a pre decimal and or post decimal zero. 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 re movetrailingzeros(AString:String;RemoveSpaces,AddPreDecimalZero,AddPos tDecimalZero:Boolean):String;var i,p:integer;begin  Result:='';  if AString='' then exit;   if RemoveSpaces then  begin    for i:=1 to length(AString) do if AString[i]<>' ' then Result:=Result+AString[i];    AString:=Result;    Result:='';  end;   p:=pos(DefaultFormatSettings.DecimalSeparator,AString);  if p<1 then p:=Length(AString);  for i:=1 to p do  begin    if AString[i]<>'0' then    begin      Result:=Copy(AString,i,MaxInt);      break;    end;  end;   If Result<>'' then  begin    if AddPreDecimalZero then if Result[1]=DefaultFormatSettings.DecimalSeparator then Result:='0'+Result;    if AddPostDecimalZero then if Result[Length(Result)]=DefaultFormatSettings.DecimalSeparator then Result:=Result+'0';  end;end; ---------------------------------------------------------------------- munair · 2022-03-13 07:00 Quote from: winni on March 13, 2022, 01:19:59 amNot only one loop but two. Look inside of IntToStr and StrToInt. Winni Sure, but if done right they are not of the programmer's concern. At least I would avoid string reassignment/reallocation within loops such as Delete() as much as possible.