问题
Pascal, How can i turn (read) a string of numbers like for example '21354561321535613' into digits and stock them in an array ?
回答1:
You can easily turn a digit into an integer by subtracting the ordinal value of '0'
. Do this in a loop and you have an integer for each digit:
var
S: string;
A: array of Integer;
I, Len: Integer;
begin
S := '21354561321535613';
Len := Length(S);
{ Reserve Len Integers. }
SetLength(A, Len);
{ Convert each digit into an integer: }
for I := 1 to Len do
A[I - 1] := Ord(S[I]) - Ord('0'); { [I - 1] because array is zero-based. }
end;
回答2:
var
Str: string;
Arr: array of Integer;
i: Integer;
Len: Integer;
begin
Str := '21354561321535613';
Len := Length(Str);
SetLength(arr, Len);
for i := 1 to Len do
Arr[i - 1] := StrToInt(Str[i]);
end;
回答3:
You can use this code to covert String to Array byte
uses crt;
var
s:string;
a:array[1..1000] of byte;
i:byte;
begin
s:='1234567';
for i:=1 to length(s) do
val(s[i],a[i]);
end.
Sorry about my English, I am student.
来源:https://stackoverflow.com/questions/41222730/pascal-string-to-array