如何将字符串拆分为固定长度子字符串数组?

How do I split a string into an array of fixed-length substrings?

我尝试制作一个有很多字符的字符串,

var
a: String;
b: array [0 .. (section)] of String;
c: Integer;
begin
a:= ('some many ... text of strings');
c:= Length(a);
(some of code)

每10个字符组成一个数组。最后是剩余的字符串。

b[1]:= has 10 characters
b[2]:= has 10 characters
....
b[..]:= has (remnant) characters   // remnant < 10

问候,

使用动态数组,运行时根据字符串的长度计算出你需要的元素个数。这是一个简单的例子:

program Project1;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

var
  Str: String;
  Arr: array of String;
  NumElem, i: Integer;
  Len: Integer;
begin
  Str := 'This is a long string we will put into an array.';
  Len := Length(Str);

  // Calculate how many full elements we need
  NumElem := Len div 10;
  // Handle the leftover content at the end
  if Len mod 10 <> 0 then
    Inc(NumElem);
  SetLength(Arr, NumElem);

  // Extract the characters from the string, 10 at a time, and
  // put into the array. We have to calculate the starting point
  // for the copy in the loop (the i * 10 + 1).
  for i := 0 to High(Arr) do
    Arr[i] := Copy(Str, i * 10 + 1, 10);

  // For this demo code, just print the array contents out on the
  // screen to make sure it works.
  for i := 0 to High(Arr) do
    WriteLn(Arr[i]);
  ReadLn;
end.

以上代码的输出如下:

This is a
long strin
g we will
put into a
n array.