Форум: "Основная";
Текущий архив: 2003.05.01;
Скачать: [xml.tar.bz2];
ВнизКак разбить строку?? Найти похожие ветки
← →
Михайлов Антон (2003-04-17 18:12) [0]у меня есть строка вида "aaa|fff|yyy|qqq" как мне получить отдельно строки "aaa" "fff" "yyy" "qqq"?
← →
MBo (2003-04-17 18:14) [1]замени | на #13#10 и StringList.text присвой
← →
BBCHa (2003-04-17 18:18) [2]могу дать функцию
← →
MBo (2003-04-17 18:22) [3]Кстати, в D6 DelimitedText есть
← →
Михайлов Антон (2003-04-17 18:34) [4]Дайте примерчик ПЛЗ...
← →
Songoku (2003-04-17 18:35) [5]function Get2Palka(S:String;Count:Integer):string;
var i,d,l,c : Integer;
begin
d:=0;
l:=0;
c:=0;
for i:=1 to Length(s) do begin
if s[i]="|" then c:=c+1;
if (s[i]="|")and(d>0) then begin l:=i; break; end;
if (s[i]="|")and(c=count) then d:=i;
end;
Result:=Copy(s,d+1,l-d-1);
end;
Вот тебе функция ...
но для неё тебе придется кое что изменить а точнее
"|aaa|fff|yyy|qqq|" . Она берёт строку от "|" до "|"!!!
Дерзай!!!
← →
BBCHa (2003-04-17 18:45) [6]//Взять подстроку из строки стоящую между определенными символами
Function StrToDatas(Var S:String;Sumb:Char;Col:Integer):String;
Var
i,j:Integer;
Log:Boolean;
SS:String;
Begin
j:=0;
Log:=True;
If Col>1
Then
Begin
j:=1;
Log:=False;
For i:=1 To Length(S) Do
If S[i]=Sumb
Then
If j<>(Col-1)
Then
Inc(j)
Else
Begin
Log:=True;
j:=i;
Break;
End;
End;
Inc(j);SS:="";
While Log AND (j<=Length(S)) Do
Begin
If (S[j]<>Sumb)
Then
Begin
SS:=SS+S[j];
Inc(j);
End
Else
Break;
End;
Result:=SS;
End;
Вот, держи, если в конце не стоит разделитель, то она возмет весь последний фрагмент
← →
Михайлов Антон (2003-04-17 19:09) [7]:/ ничё не получаеЦЦа :((
давайте по другому...
вот у мя есть файл, в нём написано
|123|5555|666|777
|asd|sdf|dfgw|fhh
|asq|wtw|yyy|uuuu
Вот это всё нужно засунуть в Стринггрид
я пытался так:
Procedure load_table(filename:string);
var str1:string;
file1:TextFile;
begin
AssignFile(file1, filename);
while not SeekEof(file1) do
begin
if SeekEoln(file1) then Readln;
Read(file1, str1);
//Тут нужно засунуть что-то, что засовывает строки из str1 вида "|11|22|33" в StringGrid.rows...
end;
CloseFile(file1);
end;
помогите лузеру :((
← →
Skier (2003-04-17 19:21) [8]>Михайлов Антон
Давай уточним :
1) Ты грузишь строки из текстового файла ?
2) строки из файла
|123|5555|666|777
|asd|sdf|dfgw|fhh
|asq|wtw|yyy|uuuu
должны "разложиться" по ячейкам StringGrid-а, т.е.
В ячейке [0,0] <- 123; [0,1] <- 5555;...[2,3] <- uuuu;
Такая логика работы ?
← →
Михайлов Антон (2003-04-17 19:26) [9]совершенно верно!
← →
Song (2003-04-17 19:35) [10]
S:="|123|5555|666|777";
Str:=TStringList.Create;
With Str Do
try
Text:=StringReplace(S,"|",#13#10,[rfReplaceAll]);
StringGrid1.Rows[0]:=Str;
finally
Str.Free;
end;
← →
Skier (2003-04-17 19:55) [11]Как вариант :
(не учитываем то, что Rows[...] может сама разбивать
строки, делаем на него...)
procedure TForm1.Button3Click(Sender: TObject);
procedure FileToGrid(const AGrid : TStringGrid;
const AFileName : String;
const ADelimiter : Char);
var
AStrList : TStringList;
ii : Integer;
jj : Integer;
AColCounter : Integer;
AStrToAdd : String;
begin
if Assigned(AGrid) then begin
if FileExists(AFileName) then begin
AStrList := TStringList.Create;
try
AStrList.LoadFromFile(AFileName);
AGrid.RowCount := AStrList.Count;
for ii := 0 to AStrList.Count - 1 do begin
AColCounter := 0;
AStrToAdd := EmptyStr;
//2 - to skip first symbol in string...
for jj := 2 to Length(AStrList[ii]) do begin
if AStrList[ii][jj] = ADelimiter then begin
Inc(AColCounter);
if AGrid.ColCount < AColCounter then
AGrid.ColCount := AGrid.ColCount + 1;
AGrid.Cells[AColCounter - 1, ii] := AStrToAdd;
AStrToAdd := EmptyStr;
Continue;
end; //if
AStrToAdd := AStrToAdd + AStrList[ii][jj];
end; //for jj
Inc(AColCounter);
if AGrid.ColCount < AColCounter then
AGrid.ColCount := AGrid.ColCount + 1;
AGrid.Cells[AColCounter - 1, ii] := AStrToAdd;
end; //for ii
finally
AStrList.Free;
end; //try
end //if
else begin
ShowMessage("File does not exist !");
end; //else
end //if
else begin
ShowMessage("Grid is not assigned !");
end; //else
end; //FileToGrid
Begin
FileToGrid(StringGrid1, "C:\###\555.txt", "|");
End;
← →
default (2003-04-17 20:06) [12]unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
MS = Array of String;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function DivideStringOnParts(const S: String; const Separator: Char): MS;
var
i: Word;
c: Integer;
pos: Byte;
begin
c := 1;
pos := 1;
for i := 1 to Length(S) do
if S[i] = Separator then begin
if Length(Copy(S, pos, i - pos)) <> 0 then begin
SetLength(Result, c);
Result[c - 1] := Copy(S, pos, i - pos);
Inc(c);
end;
pos := i + 1;
end;
if pos <> 1 then
if Length(Copy(S, pos, i - pos)) <> 0 then begin
SetLength(Result, c);
Result[c - 1] := Copy(S, pos, i - pos);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MasStr: MS;
i: Byte;
begin
Memo1.Lines.Clear;
Memo1.ScrollBars := ssVertical;
MasStr := DivideStringOnParts("|ty||||||gher|||q||fff|yyy|qqq||||yu", "|");
Caption := IntToStr(High(MasStr) + 1);
if Assigned(MasStr) then
for i := 0 to High(MasStr) do Memo1.Lines.Add(MasStr[i])
end;
end.
вот ещё пример
без учёта контекста
Страницы: 1 вся ветка
Форум: "Основная";
Текущий архив: 2003.05.01;
Скачать: [xml.tar.bz2];
Память: 0.47 MB
Время: 0.006 c