2016-12-12 3 views
1
내가 많은 문자가 문자열을 만들기 위해 노력하고

,문자열을 고정 길이 문자열의 배열로 분할하는 방법은 무엇입니까?

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 

관련,

+0

가능한 중복 : //stackoverflow.com/questions/31943087/fast-way-to-split-a-string-into-fixed-length-parts-in-delphi) –

답변

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. 
(HTTP [델파이의 고정 길이 부분으로 문자열을 분할하는 빠른 방법]의
+0

FWIW 대신'NumElem : = Len div 10; 만약 Len mod 10 <> 0이면 Inc (NumElem);'NumElem : = (Len + 9) div 10;'을 조금 더 짧게 할 수 있습니다. –

관련 문제