Dark_King
asked on
Read TXT File
If I have a file that’s look like this.
*****File******
0023fe12ceef;d2151
0023fe12f0ff;d2152
00c11098cce3;d2153
****End File******
And need to search for the first position before “;” and get the value after “;”
Ex.. I seach for 0023fe12f0ff and get d2152 as result.
I use no Form’s in my program so if you post example think of it….
*****File******
0023fe12ceef;d2151
0023fe12f0ff;d2152
00c11098cce3;d2153
****End File******
And need to search for the first position before “;” and get the value after “;”
Ex.. I seach for 0023fe12f0ff and get d2152 as result.
I use no Form’s in my program so if you post example think of it….
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ha, Cesario was faster, but sorting and binary search should be faster if you do much searching.
Hi Dark King,
so dont work with forms, so replace the Procedure with
Procedure FindItems;
.....
.....
End;
good luck
so dont work with forms, so replace the Procedure with
Procedure FindItems;
.....
.....
End;
good luck
This is a very rough answer, but this method has served me well since I started Delphi Programming. It involves using for loops to get the beginning and after portions of a two-piece string.
-------------------------- ---
Function GetLeftSide(s:string): string;
var
rstring: string;
x: integer;
begin
for x := 1 to (StrLen(Pchar(s))) do
begin
if s[x] = ';' then
begin
Result:=rstring;
Break;
end;
rstring := rstring + s[x];
end;
end; // GetLeftSide
Function GetRightSide(s:string): string;
var
rstring: string;
x: integer;
begin
for x := (StrLen(Pchar(s))) downto 1 do
begin
if s[x] = ';' then
begin
Result:=rstring;
Break;
end;
rstring := s[x] + rstring;
end;
end; // GetRightSide
----------------
Both of these functions will return a string, and so first you read the next line of text from the file and pass it to the functions.
Ok so now you need to compare it. Lets have a look at the first line of your text file you posted.
0023fe12ceef;d2151
and you're searching through the file for the "0023fe12ceef" part of it. you read in the first line of the text file and say
if GetLeftSide(stringreadfrom file) = 'mySearchString' then
myDestinationString := GetRightSide(stringreadfro mfile)
This will see if the left-hand side of the current line in the file matches the one you are searching for. if it does, then
it puts the bit after the ; into the variable myDestinationString
Hope this helps
--------------------------
Function GetLeftSide(s:string): string;
var
rstring: string;
x: integer;
begin
for x := 1 to (StrLen(Pchar(s))) do
begin
if s[x] = ';' then
begin
Result:=rstring;
Break;
end;
rstring := rstring + s[x];
end;
end; // GetLeftSide
Function GetRightSide(s:string): string;
var
rstring: string;
x: integer;
begin
for x := (StrLen(Pchar(s))) downto 1 do
begin
if s[x] = ';' then
begin
Result:=rstring;
Break;
end;
rstring := s[x] + rstring;
end;
end; // GetRightSide
----------------
Both of these functions will return a string, and so first you read the next line of text from the file and pass it to the functions.
Ok so now you need to compare it. Lets have a look at the first line of your text file you posted.
0023fe12ceef;d2151
and you're searching through the file for the "0023fe12ceef" part of it. you read in the first line of the text file and say
if GetLeftSide(stringreadfrom
myDestinationString := GetRightSide(stringreadfro
This will see if the left-hand side of the current line in the file matches the one you are searching for. if it does, then
it puts the bit after the ; into the variable myDestinationString
Hope this helps
? why is hear an accepted and a proposed answer ?
ASKER
Don't now
ASKER
A Accepted Cesario Answer
cause jamesbooker proposed the Answer and Dark king gave me the points
Cesario
Cesario
Agreed. Like I said, my answer was a little rough round the edges but I knocked it up from the top of my head without a compiler on this machine. The advantage of mine over Dark King's is that mine doesn't need the strings unit. His is a lot smaller though. :)
ASKER
Sorry for not even try your example jamesbooker,
I already start to use Cesario
Cesario is it possible to use a second position.
0023fe12ceef;d2151;d215-1
0023fe12f0ff;d2152:d215-2
00c11098cce3;d2153;d215-3
And get to separate result.
Ex.. I search for 0023fe12f0ff and get d2152 and d215-2 in to String1 and String2
I already start to use Cesario
Cesario is it possible to use a second position.
0023fe12ceef;d2151;d215-1
0023fe12f0ff;d2152:d215-2
00c11098cce3;d2153;d215-3
And get to separate result.
Ex.. I search for 0023fe12f0ff and get d2152 and d215-2 in to String1 and String2
If the file is not too big then load it with LoadFromFile.
after that sort the list. Setting sorted to True should be enough.
Then you can do a binary search for the index value.
If you replace the ';' by '=' you can use Names and Values of the TStringList to make it even easier.