PASCAL string to array

▼魔方 西西 提交于 2019-12-12 04:33:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!