我尝试制作一个有很多字符的字符串
b[1]:= has 10 characters
b[2]:= has 10 characters
....
b[..]:= has (remnant) characters // remnant < 10
并使其成为每10个字符的数组。最后是字符串的残余。
string = "abcdef"
string.each_char do |character|
character = "A"
end
puts string #=> "abcdef"
的问候,
答案 0 :(得分:2)
使用动态数组,并根据字符串的长度计算运行时所需的元素数。这是一个快速的例子:
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.