sOrigine := '20|1010100|prova|12,10||| ';
dovresti immagazzinare la stringa senza i separatori
sOrigine, sDestinazione: String;
i: integer;
potresti usare StringReplace per sostituire ! con spazio
sDestinazione := StringReplace(sOrigine, ' ! ', ' ', [rfReplaceAll, rfIgnoreCase]);
oppure fare un ciclo per eliminare un separatore alla volta,
una volta che hai le stringe senza separatori, non ci saranno problemi per inserirle in un db.
qualcosa di simile
i := Pos( '!', sOrigine,)
if i > 0 then
begin
sDestinazione := Copy(sOrigine, 1, i-1);
sOrigine := Copy(sOrigine, 1 , i-1);
end;
oppure cercare gli ultimi 3 sapratori
i := Pos( '!!!', sOrigine,)
qualcosa del genere:
var
slSrc, slFields: TStringList;
scan: integer;
SQL: string;
begin
slSrc := TStringList.Create;
slFields := TStringList.Create;
try
slSrc.LoadFromFile('...filename...');
for scan := 0 to slSrc.Count - 1 do
begin
slFields.Clear;
StrToStrings(slSrc[scan], '|', slFields, False);
SQL := Format('INSERT INTO TABLE (CAMPO1, CAMPO2, CAMPO3, CAMPO4) VALUES (''%s'', ''%s'', ''%s'', ''%s'');',
[ slFields[0], slFields[1], slFields[2], slFields[3] ]);
end;
finally
slFields.Free;
slSrc.Free;
end;
end;
il problema è che StrToStrings la trovi nelle jedi (JCL per l'esattezza, unit JclStrings)... e emh... sembra che usarle in fpc non sia così immediato :(
però ho trovato una implementazione che secondo me si adatta facilmente e se ci perdi tempo (pochissimo, ad occhio per compilarla non ci vorrà molto) hai una ottima funzione che riuserai sicuramente...
procedure StrToStrings(const S, Sep: String; const List: TStringList; const AllowBlank: Boolean);
var
I, J, L, M: Integer;
Left: String;
begin
if List <> nil then
List.Clear;
if (List = nil) or (S = '') or (Sep = '') then
Exit;
L := Length(Sep);
M := Length(S);
I := 1;
J := PosEx(Sep, S, 1);
while (J > 0) do begin
Left := Copy(S, I, J - I);
if (Left <> '') or AllowBlank then
List.Add(Left);
I := J + L;
J := PosEx(Sep, S, I);
end;
if I < M then begin
Left := Copy(S, I, M - (I - 1));
if (Left <> '') or AllowBlank then
List.Add(Left);
end;
end;
Edit:
come sento la mancanza delle jedi...
Ciao,
se non ricordo male:
var
obje: TStringList;
begin
obje := TStringList.Create;
obje.Delimiter := '|';
obje.DelimitedText := '20|1010100|prova|12,10|||';
Arrivati qui:
obje[0] sarà = a 20
obje[1] sarà = a 1010100
e così via
end;
Ciao, Mario
Le hai modificate al volo o il forum le ha modificate?
function StrTokenCount(S: String; Seperator: Char): Integer;
var i: integer;
begin
Result := 0;
for i := 1 to Length(s) do
if s[i] = Seperator then Inc(Result);
end;
Mai pensato di usare i puntatori? Sono un pelino più veloci ;)
var
c : PChar;
begin
Result := 0;
C := PChar(S);
while C^ <> #0 do
begin
if C^=separator then inc(result);
inc(C);
end;
end;
il codice l'ho modificato al volo ;)
function StrTokenAt(const S:String; Seperator: Char; At: Integer): String;
var
C, Start : PChar;
begin
Result := '';
C := PChar(S);
Start := C;
inc (C, at);
if (C-Start) > Length(S) then
// controllo di non essere uscito dai margini della memoria assegnata.
raise Exception.Create('indici mauchi'); // <- Magari un messaggio più serio
Start := C;
while (C^ <> #0) and (C^<>Separator) do
begin
inc(C);
end;
SetString(Result, Start, C-Start);
end;