2

If I have a string as '0000FFFF' or '0000F0F0', how can have the output be respectively 'FFFF' and 'F0F0', deleting the non-significant 0 from it?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Marcello Impastato
  • 2,263
  • 5
  • 30
  • 52
  • 1
    More elegant would probably be not to use strings to store hexadecimal text representation of your data. – Ondrej Kelle Oct 07 '11 at 14:55
  • Of course, but i need use string becouse working with integer over 64 bit and managing it using string. – Marcello Impastato Oct 07 '11 at 14:58
  • I would look for a library to work with big integers, maybe [this](http://sourceforge.net/projects/bigint-dl/) helps. – Ondrej Kelle Oct 07 '11 at 15:08
  • I looked it must time ago, but not work with delphi xe2 :( – Marcello Impastato Oct 07 '11 at 15:22
  • GMP is a library for big integers and floats. Here a link which points to GMP and a Delphi wrapper. [fast-bigfloat-unit-for-delphi](http://stackoverflow.com/questions/7370657/fast-bigfloat-unit-for-delphi/7371696#7371696) – LU RD Oct 07 '11 at 15:36

4 Answers4

6

This function will strip leading zeros:

function StripLeadingZeros(const s: string): string;
var
  i, Len: Integer;
begin
  Len := Length(s);
  for i := 1 to Len do begin
    if s[i]<>'0' then begin
      Result := Copy(s, i, Len);
      exit;
    end;
  end;
  Result := '0';
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • this solution is much performant respect mine solution infact not check 0 after first significant number. I asked only if in delphi was present some instruction native that do it. I have thinked to regular expression,but with delphi it little hard for me. – Marcello Impastato Oct 07 '11 at 15:48
  • 1
    Regex is perhaps too heavyweight for this problem. I think the function above is a pretty canonical solution. – David Heffernan Oct 07 '11 at 15:49
  • Attracting plenty of downvotes today. I wonder why. Perhaps I made a mistake with this code but I just can't find it. – David Heffernan Oct 07 '11 at 16:58
  • Ken White is also suffering from strange downvoting - but in his case is a systematic thing (appear that's a downvoter stalker on his case). – Fabricio Araujo Oct 07 '11 at 19:31
  • @Fabricio I think everyone knows who my stalker is! – David Heffernan Oct 07 '11 at 19:45
  • There's really some childish people here - why such disposition to do an pointless activity like downvote stalking is a question that really bugs me. – Fabricio Araujo Oct 07 '11 at 19:52
  • @Fabricio Araujo, i wish there was verbose discussion about "unfair upvotes" :-) Negative was from me, no commenting because i neither wish to argue, nor to sustain revenge downvoting attack. This problem splits into two parts and solves in 4 LoC (or 5 with explaining variable). Not surprising what i dislike clumsy solutions with involves moving data back and forth with no purpose. – Premature Optimization Oct 08 '11 at 00:10
  • @downvoter Thanks for taking the time to explain the downvote. What I don't understand is how this is clumsy. Could you elaborate. Could you post your non clumsy version as an answer and I will be pleased to upvote. – David Heffernan Oct 08 '11 at 07:27
  • 1
    @David Heffernan, i should request upvotes in advance to have some guarantee you will be pleased :-) Anyway, code follows. – Premature Optimization Oct 09 '11 at 01:08
1
Format('%X', [StrToInt('$' + number)])
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • it work until "number" is under high(Int64), but in my case not is applicable. I thinked to regular expression or something of more elegant present in delphi. But i have some difficult about it. – Marcello Impastato Oct 07 '11 at 15:04
0
function mystrip(Value: string): string; 
var 
  Flag: Boolean; 
  Index: Integer; 
begin 
  Result := ''; Flag := false; 
  for Index := 1 to Length(Value) do 
  begin 
    if not Flag then 
    begin 
      if (Value[Index] <> #48) then 
      begin 
        Flag := true; 
        Result := Result + Value[Index]; 
      end 
    end 
  else 
    Result := Result + Value[Index]; 
  end; 
end; 
Marcello Impastato
  • 2,263
  • 5
  • 30
  • 52
  • For what it's worth this will fail for '0'. Presumably you do want the zero then because it is significant. Also it is pretty inefficient to add one character at a time to the string. One final point about my version is that it returns '0' for the empty string. You may want to tweak that, or it may not matter a jot to you. – David Heffernan Oct 07 '11 at 15:48
  • 1
    +1 I don't understand why this has been downvoted. Boo hiss to the downvoters! – David Heffernan Oct 07 '11 at 18:45
0

This problem splits into two parts and solves in 4 LoC (or 5 with explaining variable).

licensed under GPL

function TrimLeading(const S: string): string;
var
  I: Integer;
begin
  I := 1;
  while (I < Length(S)) and (S[I] = '0') do
    Inc(I);
  Result := Copy(S, I, MaxInt);
end;
Premature Optimization
  • 1,917
  • 1
  • 15
  • 24