Well you'll need a function that will take the number out of your '[0] yy(z)' string:
//Note: This code is UNTESTED
function CleanNumber(aString) : integer;
var
PosOpenBracket : integer;
PosCloseBracket : integer;
begin
result := -1;
PosOpenBracket := AnsiPos('[', aString); //produces 0 if not found
PosCloseBracket := AnsiPos(']', aString); //produces 0 if not found
result := MidStr(aString, PosOpenBracket, PosCloseBracket - PosOpenBracket);
//Note: if the above line isn't working, try this:
//result := MidStr(aString, PosOpenBracket - 1, PosCloseBracket - PosOpenBracket - 1);
end;
Then you could iterate through your array, and insert into a new array after cleaning
var
i : integer;
newarray : array of integer;
begin
setlength(newarray, 0);
for i := 0 to length(A) do
begin
setlength(newarray, length(newarray + 1));
newarray[i] := CleanNumber(A[i]);
end;
end;
Note: it may be more effecient to not set the length of the array every time you add a new element. IF you know the length of the array must be the same as the length of A, then just set it once:
var
i : integer;
newarray : array of integer;
begin
setlength(newarray, length(A));
for i := 0 to length(A) do
newarray[i] := CleanNumber(A[i]);
end;
function CleanNumber(aString) : integer;
var
PosOpenBracket : integer;
PosCloseBracket : integer;
begin
Result:=-2; // default value for brackets errors
PosOpenBracket := Pos('[', aString);
if PosOpenBracket<1 Then Exit;
PosCloseBracket := PosEx(']', aString, PosOpenBracket);
if PosCloseBracket <1 Then Exit;
Result:=
StrToIntDef(Trim(
Copy(aString,PosOpenBracket+1,PosCloseBracket-PosOpenBracket-1)
),-1); // -1 is the value for invalid int value inside the brackets, spaces non-withstanding
end;
PosCloseBracket := PosEx(']', aString, PosOpenBracket [b]+1[/b] );
Then Copy just do one Move operation with the correct number of chars.[ 10 ] alongstring(anevenlongerstthe difference in performance would start to be very noticeablering)
[ 10 ] 2be3(whatever7)would return 10237 as value, which is not correct. only by checking bracket positions such inconvenience can be avoided.
function IsNumeric(const AValue: string): Boolean;
var
I: Integer;
begin
for I := 1 to Length(AValue) do
case AValue[I] of
'0'..'9':
;
else
begin
Result := False;
Exit;
end;
end;
Result := Length(AValue) > 0;
end;
function CleanNumber(const AValue: string): Integer; //using IsNumeric
var
I: Integer;
temp: string;
begin
temp := '';
for I := 0 to Length(AValue) -1 do
if IsNumeric(AValue[I]) then
temp := temp + AValue[I];
Result := StrToIntDef(Temp, -1);
end;
function CleanNumber(const AValue: string) : integer; //using Pos
var
PosOpenBracket: Integer;
PosCloseBracket: Integer;
begin
Result := -1;
PosOpenBracket := Pos('[', AValue);
if PosOpenBracket < 1 then
Exit;
PosCloseBracket := Pos(']', AValue);
if PosCloseBracket < 1 then
Exit;
Result:= StrToIntDef(Trim(Copy(AValue, PosOpenBracket +1 ,PosCloseBracket-PosOpenBracket -1)), -1);
end;
procedure CopyArrayElements;
var
I: Integer;
Destination: array of integer;
begin
SetLength(Destination, Length(SourceArray));
for I := 0 to Length(SourceArray) -1 do
Destination[I] := CleanNumber(SourceArray[I]);
end;
function IsNumeric(const AValue: string): Boolean;
var i: Integer;
begin
i:=Length(AValue);
Result:=i>0;
While (i>0) And Result do
begin
Result:=(AValue[i]>='0') And (AValue[i]<='9');
Dec(i);
end;
end;
var
Str:TStringlist;
begin
Str := TStringlist.Create;
// any chars other than 0..9 will be considered as separators
ExtractStrings(['[',']','(',')','a'..'z','A'..'Z'],[' '],'[ 123 ] yy(zzz)',Str);
// for showing the first item which will be 123
showmessage(IntToStr(StrToInt(Trim(Str[0]))));
Str.Free;
ExtractStrings( [ 0..#255] - ['0'..'9'],[],'[ 123 ] yy(zzz)',Str);
Title | # Comments | Views | Activity |
---|---|---|---|
Component or function to create an ICS-file | 2 | 90 | |
Get a Metafile from Clipboard and convert it into a PNG to save to file or stream. | 2 | 84 | |
Delphi 10 Seattle Dataset Actions | 5 | 71 | |
Delphi inherited method | 6 | 36 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
11 Experts available now in Live!