作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试制作一个具有很多字符的字符串,
var
a: String;
b: array [0 .. (section)] of String;
c: Integer;
begin
a:= ('some many ... text of strings');
c:= Length(a);
(some of code)
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.
关于delphi - 如何将字符串拆分为固定长度的子字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41093480/
我是一名优秀的程序员,十分优秀!