2

I have some strings which are dynamically created at run time.

The values are generated in sequence with an underscore, leading zeroes and then the sequence number. The format looks like so:

_00001  
_00002  
_00003  
_00004

_00098  
_00099  
_00100  
_00101

I would like to extract the number part as Integer, ignoring the underscore and leading zeroes.

StringReplace can easily remove the underscore part, but how can I extract the number part as I have no way of knowing what the values are?

Some example outputs of how such a function would work:

_00003 = 3
_00098 = 98
_00482 = 482
_07218 = 7218
_14005 = 14005
_37585 = 37585
_69732 = 69732

StringReplace and Pos is what I am thinking, but I am not always sure as to how to fully use Pos, and the StringReplace method could possibly be messy.

I think all I need to do is check for the first number that is not 0, but maybe I am confusing myself again so really any pointers are welcome.

2
  • 3
    Zeroes in front should be ignored. Have you tried StrToInt()? Commented Sep 25, 2013 at 16:42
  • 10
    StrToInt(Copy(S, 2, MaxInt)) Commented Sep 25, 2013 at 16:43

2 Answers 2

13

You should use

StrToInt(Copy(S, 2))

Indeed, Copy(S, 2) returns all characters of S except the first one, so you'd get 00003, 00098, 00482, etc. And these you can easily convert to the corresponding integers using StrToInt.

Sign up to request clarification or add additional context in comments.

14 Comments

How come I never knew you could exclude the third argument of Copy? All those times I typed Copy(S, 2, Length(S));
@JerryDodge funny you mention this because the code in Andreas answer worked in XE, then I tried also in Lazarus and it failed, unless you include the Length(S) part. For some reason I like the look of keeping Length(S) in there anyway :)
@FreeConsulting, essentially yes, but MaxInt is better because it's constant, so you don't bother with determining of the string length at runtime :-)
@TLama, I do agree. But I was talking about attempting to Copy Length(S) characters from the string S starting from 2nd(!) character. This always results copying to the end of string, just as same as attempting to Copy MaxInt characters.
@alcalde, you're right. However, in this particular case, if you were having a string longer than 2147483648 of digits to represent a number, I doubt you will have a numeric data type to store that large number for the StrToInt conversion (it would have been data type larger than Int17179869184 keeping the bit naming convention). As a compromise I would suggest to have a constant like MaxStr with a length limit of the string data type.
|
3

You can use something like this :

function YourTypeStrToInt(const StrNum: String): Integer;
begin
  Result := StrToInt(Trim(StrNum.Remove(0, 1)));
end;

for the old Delphi version:

function YourTypeStrToInt(const StrNum: String): Integer;
var
  P: PChar;
begin
 P:=PChar(Trim(StrNum));
 Inc(P);
 Result:=StrToInt(P);
end;

2 Comments

or Result:=StrToInt(PChar(Trim(StrNum)) + 1); +1
+1 for coming back with an updated answer with compatible example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.